PowerShell Cookbook

Search

Categories

 

On this page

Archive

Blogroll

Disclaimer
I work for Microsoft.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 216
This Year: 16
This Month: 0
This Week: 0
Comments: 523

Sign In

 Friday, May 05, 2006
Friday, May 05, 2006 6:34:01 PM (Pacific Daylight Time, UTC-07:00) ( )

One of the things that people often struggle with when they try to use PowerShell scripts as targets of Scheduled Tasks, or launch PowerShell scripts from cmd.exe is the following error message:

C:\Program Files\Windows PowerShell\v1.0>powershell.exe "c:\temp\has space\test.ps1"
'c:\temp\has' is not recognized as a cmdlet, function, operable program, or script file.
At line:1 char:12
+ c:\temp\has  <<<< space\test.ps1

(By the way, you can speed this up by using the -noprofile parameter to powershell.exe)

This is because Powershell doesn’t natively support a parameter for a script to run.  The default command-line argument is “-command,” which defines the command as though you had typed it at the prompt.

For example:

C:\temp\monad>powershell "2+2"
4

This happens to work on script names with no spaces or quotes, as our interpreter interprets that as a command execution.  For scripts with spaces and quotes, you are doing the equivalent of:

[C:\temp]
PS:13 > c:\temp\has space\test.ps1
'c:\temp\has' is not recognized as a cmdlet, function, operable program, or script file.
At line:1 char:12
+ c:\temp\has  <<<< space\test.ps1
 

[C:\temp]
PS:14 > "c:\temp\has space\test.ps1"
c:\temp\has space\test.ps1

Suggestion: Did you mean to run the command in quotes?  If so, try using & "<command>"

So, the solution is:

[C:\temp]
PS:15 > & 'C:\temp\has space\test.ps1'
Hello World

Or,

C:\temp\monad>powershell "& 'c:\temp\has space\test.ps1'"
Hello World

This is something that we know to be a usability issue.  We’re tracking it internally, but a bug (and votes) on Ms Connect would help us prioritize this properly.

Saturday, May 06, 2006 11:32:41 PM (Pacific Daylight Time, UTC-07:00)
I'm looking for the PS equivalent of:

cd %userprofile%

which in cmd.exe does:

c:\Documents and Settings\Some User>

I want to do this in PS since there are so many environment variables that are paths, such as %windir@ and %systemroot% and I can't seem to do anything like:

cd &Env:Systemroot to change to the path pointed by the env variable.

gc Env:Systemroot returns C:\Windows but how do I cd there?
David Moisan
Sunday, May 07, 2006 5:19:16 AM (Pacific Daylight Time, UTC-07:00)
Hi David;

The way to access environment variables in PowerShell is "$env:SystemRoot" (no quotes needed.) The dollar sign is to reference variables, and the & sign is usually to execute something. You can find a discussion of this in the help topic, about_Environment_variable.

I'm sorry that this sounds like magic sprinkling sugar right now -- I'll post a blog soon describing more detail about it.
Friday, June 13, 2008 6:48:07 PM (Pacific Daylight Time, UTC-07:00)
Thanks for posting this, I was looking every where to find out why my xp sched.taks/cronjob was'nt running

C:\temp\monad>powershell "& 'c:\temp\has space\test.ps1'"

This detail is very poorly documented. Searched ms a bunch.
thanks again.
Name
E-mail
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike, strong, sub, super, u)  

Enter the code shown (prevents robots):