An example of what fails.
powershell.exe -Command "c:\path with space\script1.ps1 arg1"
However by using the "& - call operator" you can successfully.
This example works.
powershell.exe -Command "& 'c:\path with space\script1.ps1' arg1"
Note: When passing the command line argument "-WindowsStyle" to powershell.exe you have to place it before the "-Command" argument. If "-Command" comes first "-WindowStyle" doesn't work.
Here's an example
powershell.exe -WindowsStyle Hidden -Command "& 'c:\path with space\script1.ps1' arg1"
Side Note: After posting this my brother was like "Of course thats the way around it" and I've swear i asked him the last time it came up.
Links
http://www.leeholmes.com/blog/2006/05/05/running-powershell-scripts-from-cmd-exe/
Thanks. This is the one I was looking for
ReplyDeleteThanks for writing..I had got the same issue, and your fix resolved it. Thanks again!!
ReplyDeleteFinally...and now I can move on with my life... On a side note http://technet.microsoft.com/en-us/library/dd315276.aspx is also pretty useful.
ReplyDeleteDude, you rock. I was not able to find this information anywhere else. Thanks a bunch!
ReplyDeleteI have got space characters not in file path but in Azure Subscription name (Windows Azure MSDN Subscription), my command is like this:
ReplyDeleteC:\file.ps1 -subscriptionName "Windows Azure MSDN Subscription"
This does not fail but does not shows the correct subscriptioname when I do an Write-Output
Using your method:
C:\file.ps1 -subscriptionName "& 'Windows Azure MSDN Subscription'"
This fails with message - 'Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.'
Can you please help me find a solution to this.
Try...
DeleteC:\file.ps1 -subscriptionName 'Windows Azure MSDN Subscription'
Worked for me.
Also,
powershell.exe -Command "& 'c:\path with space\script1.ps1' arg1"
didn't work for me but
powershell.exe -Command 'c:\path with space\script1.ps1 arg1 '
did