Thursday, January 2, 2014

Simple Powershell That Builds A List From User Input



This script builds a list from user input.   For me this list is used as a source for configuring folders and xml config files with other Powershell scripts used in a custom process on my job.
The scripts keeps prompting for inputs until quit.


#Declare Variables (Optional)
$DestList=”c:\mypath\list.txt  The list file being built


clear (clears the screen)


echo “Start Building the List”


new-item $DestList -type file -force   Create the file and if it already exists force it to be recreated


$response = “”   
do
{
echo “”
echo “”
$response = Read-Host “Enter a list item or Q when done”   Can use Q or q
if ($response -ne “Q”)
{
echo $response >> $DestList
}
until ($response -eq “Q”)

# The next section is optional

clear
echo "Here is the list"
echo ""
cat $DestList
echo ""
echo ""
echo "If the list is not correct either re-run script or directly edit the new file"
start-sleep -s 10  (gives time for user to see list and message before script quits)


____________Full Script_____________________
#Declare Variables
$DestList=”c:\mypath\list.txt  
clear
echo “Start Building the List”
new-item $DestList -type file -force  
$response = “”   
do
{
echo “”
echo “”
$response = Read-Host “Enter a list item or Q q when done”  
if ($response -ne “Q”)
{
echo $response >> $DestList
}
until ($response -eq “Q”)

# The next section is optional

clear
echo "Here is the list"
echo ""
cat $DestList
echo ""
echo ""
echo "If the list is not correct either re-run script or directly edit the new file"
start-sleep -s 10

No comments:

Post a Comment