My Technical Notes

Sunday, 20 December 2015

Start-Process PowerShell.exe with arguments

Given a script called `TestScript.ps1`:


Param([string]$message)

Write-Host $message
We can call the script using `Start-Process` as follows:

Start-Process powershell.exe ".\TestScript.ps1 'Hello World'"

What is important in the above line is that the string arguments with white-spaces must be enclosed in single quotes - not double quotes.

If we want the new PowerShell window to remain open, then we can supply the -NoExit argument at the beginning:


Start-Process powershell.exe "-NoExit .\TestScript.ps1 'Hello World'"

If we want to pass in the arguments individually, we must always pass in the string arguments (especially those containing spaces) surrounded by single-quotes:


Start-Process powershell.exe -ArgumentList @("-NoExit", '.\TestScript.ps1', "'Hello World'")

No comments: