How Windows PowerShell handles blank space in the file path?
Here is a little script to get AIM process on the computer, nothing special about it.
get-process | where {$_.ProcessName -match "aim*"}
PS C:\> c:\ps\test.ps1
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
521 13 63760 15024 180 9.11 3904 aim6
If I save it in C:\Documents and Settings\Ying\My Documents\PS\ and run it
PS C:\> C:\Documents and Settings\Ying\My Documents\PS\test.ps1
The term 'C:\Documents' is not recognized as a cmdlet, function, operable progr
am, or script file. Verify the term and try again.
At line:1 char:13
+ C:\Documents <<<< and Settings\Ying\My Documents\PS\test.ps1
I get an error, this is because there are blank spaces in the path
In order to run it – I have to do the following:
PS C:\Documents and Settings\Ying\My Documents\PS> & "C:\Documents and Settings\Ying\My Documents\PS\test.ps1"
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
521 13 63760 15032 180 9.13 3904 aim6
Now try to do the same from start-run command
Powershell.exe -noexit c:\ps\test.ps1
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
521 13 63760 15024 180 9.11 3904 aim6
The result is expected. but what about doing this:
powershell.exe -noexit & "C:\Documents and Settings\Ying\My Documents\PS\test.ps1"
The term 'C:\Documents' is not recognized as a cmdlet, function, operable progr
am, or script file. Verify the term and try again.
At line:1 char:2
+ & <<<< C:\Documents and Settings\Ying\My Documents\PS\test.ps1
PS C:\PS>
How we do this from start -run?
powershell.exe -noexit & 'C:\Documents and Settings\Ying\My Documents\PS\test.ps1'
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
521 13 63760 15024 180 9.11 3904 aim6
So if you want to run a powershell script with blank space in it’s path, you have to add “” if you run it in PowerShell console; add ‘’ if you want to run it from start -run command. Of course, don’t forget the &!
Confused? you are not alone!