Powershell argument list and double + spaces
Can anyone explain this? I run the following Powershell commands and the double spaces between the two "tests" are converted to one space. Using PS 3.0.
Start-Process Powershell -ArgumentList "-Command Write-Host test test;pause"
Start-Process Powershell -ArgumentList "-Command Write-Host 'test test';pause"
Start-Process Powershell -ArgumentList "Write-Host 'test test';pause"
Below is the script call (which only contains the $ String parameter and the Write-Host $ String; pause), but works in one session:
& C:\Test\Test.ps1 -String 'test test'
But below is replacing double space with one:
Start-Process Powershell -ArgumentList "& C:\Test\Test.ps1 -String 'test test'"
I need to be able to run another PS script outside of the current session and pass the script arguments, which may include double spaces like these. Thank!
source to share
As far as I can tell, this is because double quotes are not passed to arguments before being sent to PowerShell. This can cause the shell to hide whitespace (how they parse arguments) before PowerShell sees them.
So, I tried this test:
Start-Process powershell.exe -ArgumentList "-NoExit -Command Write-Verbose '1 2 3 .' -Verbose"
It failed like you, with no gaps.
So, in a new typed PowerShell window, I ran this:
gwmi Win32_Process -Filter "ProcessId = '$([System.Diagnostics.Process]::GetCurrentProcess().Id)'" | select -ExpandProperty CommandLine
Result:
"C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit -Command Write-Verbose '1 2 3 .' -Verbose
See how there are no double quotes around the command? The shell doesn't seem to care about single quotes, so every space in your command causes the shell to treat it as a new argument (it's mostly split into spaces, so multiple consecutive spaces are compressed, unless they see a double-quoted string treated as one argument).
You can fix this by inserting the quotes yourself.
Start-Process Powershell -ArgumentList "`"Write-Host 'test test';pause`""
source to share