input from file - quick question

Welcome Forums General PowerShell Q&A input from file - quick question

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5502

    Points: 0
    Rank: Member

    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.

The topic ‘input from file - quick question’ is closed to new replies.