Run Powershell with parameters from batch file

5,116

Batch script that accepts arguments

Here's a batch script with arguments that are set as variables, and those variables passed to a PowerShell script and executed. You can execute a PowerShell script with batch this way.

Batch Script Example

Scale the batch arguments up with subsequent SET arg#=%~#

@ECHO ON

SET arg1=%~1
SET arg2=%~2
SET arg3=%~3
SET arg4=%~4

SET PSScript=C:\Users\User\Desktop\Test.ps1

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%' '%arg1%' '%arg2%' '%arg3%' '%arg4%'"
EXIT /B

Powershell script that accepts arguments

Scale the PowerShell arguments up with subsequent $arg#=$args[#]

Example PowerShell Script

$arg1=$args[0]
$arg2=$args[1]
$arg3=$args[2]
$arg4=$args[3]

Write-Host "$arg1 is a beauty!!"
Write-Host "$arg2 is cool!!"
Write-Host "$arg3 has body odor!!"
Write-Host "$arg4 is a beast!!"

Tying it together

  • Pass arguments to the batch script:

  • c:\users\user\desktop\test.cmd "Princess" "Joe" "Akbar" "WeiWei"

  • Pass arguments to the PowerShell script:

  • Powershell -ExecutionPolicy Bypass -Command "& 'C:\Script\Path\psscript.ps1' 'Princess' 'Joe' 'Akbar' 'WeiWei'
    

Supporting Resources

Share:
5,116

Related videos on Youtube

Marcelo V
Author by

Marcelo V

Updated on September 18, 2022

Comments

  • Marcelo V
    Marcelo V over 1 year

    I have this PowerShell command that I run from directly from the PowerShell terminal on my Windows 10 system and it works great.

    PowerShell

    Get-AppxPackage | % { Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppxManifest.xml" -verbose }
    

    I'm trying to create a batch file to automate this PowerShell and I'm having trouble. I tried a bunch of different things but I'm way off in my testing.

    I'm wondering if someone has some advice or examples of running PowerShell commands with batch files and using parameters.

    • Frank Thomas
      Frank Thomas about 3 years
      well, if you put your ps script in a .ps1 file, it should be somthing like powershell scriptfile.ps1. of course this assumes that you won't need to pass or recieve arguments/return values from within the PS script.
    • Marcelo V
      Marcelo V about 3 years
      thanks but I´m newbie in DOS and POWERSHELL.So how would this .ps1 file file look and how would this .bat file look? Can you give me a practical example using my powershell command ?
    • SimonS
      SimonS about 3 years
      in the bat file: powershell -command "Get-AppxPackage | foreach { Add-AppxPackage -DisableDevelopmentMode -Register '$($_.InstallLocation)\AppxManifest.xml' -verbose }" - however I don't think you need a batch file at all. You can execute a .ps1 file directly with powershell and you can also run PowerShell directly from scheduled task.
    • Io-oI
      Io-oI about 3 years
      Your args and your batch!?
  • Marcelo V
    Marcelo V about 3 years
    thanks but if I use this in a .bat file or cmd command line, this error appear "'%' is not recognized as an internal command or external, an operable program or a batch file."
  • Frank Thomas
    Frank Thomas about 3 years
    you'll have to play with the quotes. I'd try putting the whole ps command in double quotes, and escape the double quotes in the command itself.
  • Marcelo V
    Marcelo V about 3 years
    I solved creating a script , .ps1 file , in Powershell ISE and after this I went to Powershell window and type this command to allow script to run : Set-ExecutionPolicy RemoteSigned and create a .bat file with this line : Powershell.exe -executionpolicy remotesigned -File "ps1_file_path\ps1_file_name.ps1" and all works fine.
  • Marcelo V
    Marcelo V about 3 years
    nope with quotes same problem. i solved using the method above
  • JG7
    JG7 about 3 years
    @MarceloV if the issue is with the alias, then sub in ForEach-Object.
  • Vomit IT - Chunky Mess Style
    Vomit IT - Chunky Mess Style about 3 years
    @MarceloV .... FYI.... the arguments are optional so those aren't needed in the first script that you can use to execute any PS as specified in the PS script file you point that batch file logic to execute. That will be your .cmd or .bat file. You could even pass in the PS script full path as an argument and have one script that executes any PS script you pass to it. The possibilities are really endless with what you can do.