Pass parameter from a batch file to a PowerShell script
batch-file, powershell
Solution
Let's say you would like to pass the string `Dev` as a parameter, from your batch file:
powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 Dev"
put inside your powershell script head:
$w = $args[0] # $w would be set to "Dev"
This if you want to use the built-in variable `$args`. Otherwise:
powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 -Environment \"Dev\""
and inside your powershell script head:
param([string]$Environment)
This if you want a named parameter.
You might also be interested in returning the error level:
powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 Dev; exit $LASTEXITCODE"
The error level will be available inside the batch file as `%errorlevel%`.
Problem
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?