Ying Li(MVP) at myITforum.com

PowerShell & System Center

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!

 

Posted: Sep 26 2007, 10:29 PM by yli628 | with 3 comment(s)
Filed under:

Comments

KirkAMunro said:

I figured out the reason why your first command didn't work and decided to write about it in my blog.  Here's the actual blog entry:

kirkmunro.wordpress.com/.../post.php

Enjoy!

# September 27, 2007 11:41 PM

KirkAMunro said:

I figured out why your first command wouldn't run and decided to put the details in an entry on my blog.  Here's the link to the specific entry:

poshoholic.com/.../invoking-a-powershell-script-from-cmdexe-or-start-run

Enjoy!

(P.S. You can delete my other comment.  The URL I passed in wasn't the right one.

# September 27, 2007 11:44 PM

Invoking a PowerShell script from cmd.exe (or Start | Run) « Poshoholic said:

Pingback from  Invoking a PowerShell script from cmd.exe (or Start | Run) &laquo; Poshoholic

# September 28, 2007 12:00 AM