Thursday, February 23, 2012

Powershell–calling from command line with arguments

Here is how you can create a batch file that allows you to call a powershell script and pass parameters to it:

powershell.exe -Command "& 'c:\scriptName.ps1' -argument1 '%1' -argument2 '%2' -argument3 '%3' –argument4 %4"

And you would call the batch file like so:

"HelloWorld" "Hello World 2012" "" $true

Things to note:

The script name is quoted.

The first 3 arguments are passed in as a quoted string. This is because you may be passing in the strings with spaces in them. (Note while calling your batch file you need to use double quotes around your string to pass it to powershell.

The last argument is not being passed in as a quoted string. This is because in my case I am passing a boolean value and need it to be interpreted as such.

The above format works perfectly with Windows Task Scheduler (where your task calls the batch file and you setup the arguments in the task-action.

No comments: