Why are you running it that way? When you run powershell.exe
from inside Powershell you are forcing the arguments to go through the old Windows cmd.exe style command line processing.
Instead you should just invoke your script directly:
PS C:\scripts> .\PowershellTest.ps1 -message "Error: Test-Error"
Info: Test-Information
Error: Test-Error
This way all of the arguments will pass right through without being converted to strings, having double quotes stripped and then being re-parsed.
If you get an error because running scripts is not permitted then the answer is to change your execution policy, not to start another copy of powershell.exe
:
PS C:\scripts> .\PowershellTest.ps1 -message "Error: Test-Error"
.\PowershellTest.ps1 : File C:\scripts\PowershellTest.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\PowershellTest.ps1 -message "Error: Test-Error"
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
If you see this error then start a Powershell window using the 'run as administrator' option and enter the command:
Set-ExecutionPolicy RemoteSigned
Close the administrator powershell and restart any other powershell windows you have running and now you should be able to run locally created scripts without any problems.