Acerbity
Acerbity

Reputation: 527

powershell.exe cmd line variables

I was wondering if is possible to call a variable for use in a PowerShell script using the command line.

For instance, I have a script that I would like to run tonight. I want to run it at two times using a different input file (csv) each time.

Can I call...

powershell.exe -command {$input = <csv path>} -file <script path>

and discretely call a variable for use in the script from outside of the script? Do I just need to make two copies of the script with the variable change included in the script itself? Is there a way to do this that I am missing?

Upvotes: 2

Views: 5264

Answers (1)

Lars Truijens
Lars Truijens

Reputation: 43605

PowerShell scripts can have parameters, just as functions can. That way you can have one script and just call it with different input file names. Also see here for a similar question, here what more you can do with parameters and here for more commandline options.

Script

Param([Parameter(Mandatory=$True)] [string] $FileName)

Write-Output "Script called with $FileName"

Call

powershell.exe -file Untitled2.ps1 Test3.csv

Upvotes: 5

Related Questions