Pass parameter from a batch file to a PowerShell script

Viewed 186891

In my batch file, I call the PowerShell script like this:

powershell.exe "& "G:\Karan\PowerShell_Scripts\START_DEV.ps1"

Now, I want to pass a string parameter to START_DEV.ps1. Let's say the parameter is w=Dev.

How can I do this?

5 Answers

The answer from @Emiliano is excellent. You can also pass named parameters like so:

powershell.exe -Command 'G:\Karan\PowerShell_Scripts\START_DEV.ps1' -NamedParam1 "SomeDataA" -NamedParam2 "SomeData2"

Note the parameters are outside the command call, and you'll use:

[parameter(Mandatory=$false)]
  [string]$NamedParam1,
[parameter(Mandatory=$false)]
  [string]$NamedParam2
Related