Computing Errors Programming & Coding

Powershell – Executing commands which require quotes and variables is practically impossible!

I’ve spent the last 2 hours reading an interesting bug report/forum thread about how executing commands which require quotes and variables in PowerShell is difficult (this was originally posted on Microsoft Connect, which no longer exists; replaced with archive.org link). I’m trying to understand how to properly quote commands with parameters and interpolated paths so that they will execute as a single command.  Doesn’t seem like it should be so hard.  Here is an example of the code I’m working with:

The command I’m trying to run:

C:\Program Files\Mozilla Firefox\firefox.exe -createprofile myTest

Here is what works in MS-Dos / Windows XP command prompt:

"C:\Program Files\Mozilla Firefox\firefox.exe" -CreateProfile myTest

However, run the PowerShell version of the same command,

PS C:\Documents and Settings\nunya> “C:\Program Files\Mozilla Firefox\firefox.exe” -CreatProfile myTest

and this is what you get:

You must provide a value expression on the right-hand side of the ‘-‘ operator.
At line:1 char:48Unexpected token ‘CreatProfile’ in expression or statement.
At line:1 char:49Unexpected token ‘myTest’ in expression or statement.
At line:1 char:62

Originally published: 03/30/2012
Last Updated: 11/13/2018

0 0 votes
Article Rating
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jrich
9 years ago

the problem is that with cmd there is no such thing as a string in the sense that its not a programming language, its just designed to run things. powershell on the otherhand deals with different data types and you’ve just provided a string and didnt tell powershell what to do with the string. sure its harder, but once you dig in to it, it makes perfect sense. Perhaps i’ll add this to the wiki. thanks for the pingback.

kristobaldude
kristobaldude
9 years ago
Reply to  jrich

@jrich; Thanks for your reply. I’m definitely a Powershell beginner (only discovered PS 2 weeks ago), but coming from a Perl background I see a lot of similarities and it makes the learning curve less steep. I’ll look forward to more informative posts about powershell from you, my friend.

-CodeSlayer2010

jrich
9 years ago

Yes PowerShell has taken a lot from other technologies out there but i think the biggest difference between powershell and everything else is that it works in objects, which has given it a lot more power but has also made it more complex to work with basic stuff, which is what you’ve found here. keep at it and if you’ve got questions dont hesitate to ask on the technet powershell forums, lots of great people there!

kristobaldude
kristobaldude
9 years ago
Reply to  jrich

I agree about the advantages of objects — I started doing procedural programming, and of course you can simulate OOP in Perl, but they haven’t introduced true/complete object functionality yet. However, in my current project I use PHP and Json objects everyday. As far a Powershell, yeah TechNet has actually been my greatest resource that has really good step by step explanations that address the questions and pit-falls one may encounter during implementation of certain functions. Every language has it’s limitations, but I’m not going to give up on Powershell anytime soon. Thanks again for your reply brother, and take… Read more »