- 1 year ago
Powershell to call .bat file with arguments in a list
To call a .bat file with arguments from a list in PowerShell, you can use a foreach
loop to iterate through the list and execute the .bat file for each argument. Here's an example:
powershell$arguments = @("argument1", "argument2", "argument3") # Replace with your list of arguments
$batFilePath = "C:\path\to\your\file.bat" # Replace with the actual path to your .bat file
foreach ($arg in $arguments) {
& $batFilePath $arg
}
In the above script, $arguments
represents the list of arguments you want to pass to the .bat file. You can modify this list according to your requirements.
$batFilePath
should be set to the actual path of your .bat file. Make sure to provide the full path including the file name and extension.
The foreach
loop iterates through each argument in the $arguments
list and executes the .bat file using the &
(call operator) followed by the path of the .bat file and the argument ($arg
) passed as an argument to the .bat file.
Save the PowerShell script with a .ps1
extension (e.g., call_bat_with_args.ps1
) and run it by opening a PowerShell session and executing the script:
powershellPS C:\> .\call_bat_with_args.ps1
This will call the .bat file for each argument in the list, passing the argument as a parameter to the .bat file.