- 1 year ago
Passing parameters from batch file to PowerShell
Comment
1 / 1
You can pass parameters from a batch file to a PowerShell script by using the %*
variable to capture all the command-line arguments passed to the batch file, and then invoking the PowerShell script with those arguments. Here's how you can do it:
- Create a batch file (e.g.,
myscript.bat
) that calls the PowerShell script with the desired parameters:
batch@echo off
setlocal
set "param1=%~1"
set "param2=%~2"
powershell -ExecutionPolicy Bypass -File "path\to\your\powershell_script.ps1" "%param1%" "%param2%"
endlocal
Replace "path\to\your\powershell_script.ps1"
with the actual path to your PowerShell script.
- Create the PowerShell script (e.g.,
powershell_script.ps1
) that receives and uses the parameters:
powershellparam(
[string]$param1,
[string]$param2
)
Write-Host "Parameter 1: $param1"
Write-Host "Parameter 2: $param2"
When you run the batch file with parameters, the batch file will pass those parameters to the PowerShell script, which will then use them.
For example, if you run the batch file as follows:
batchmyscript.bat value1 value2
The PowerShell script will output:
mathematicaParameter 1: value1
Parameter 2: value2
Remember that if your parameters contain spaces or special characters, you might need to enclose them in double quotes in the batch file and escape special characters if needed.