input from file - quick question

by chz013 at 2013-02-03 21:40:30

Hi All

Could someone share a few examples on how to pass file parameter to the powershell script and perform its function reading inputs from a file as its argument ?

powershell.exe myscript filename.txt

where filename.txt contains a list of hostnames:

by DonJ at 2013-02-04 10:33:57
You put a parameter block at the beginning of the script:


Within the script, $filename will contain whatever file name was passed. You would run the script like this:

myscript filename.txt

Or more properly:

myscript -filename filename.txt

And in either case, $filename would contain "filename.txt." You could then read that file by using Get-Content.

If you want to provide the CONTENTS of the file as a parameter value:

foreach ($computer in $computername)
# $computer contains one computer name at a time

And run as:

Myscript -computername (Get-Content names.txt)

Where names.txt contains one name per line.