Home > .CodeSlayer's Posts, Developer Journal, DOS Command Line, Powershell, Technical Blogging > Powershell – Executing commands which require quotes and variables is practically impossible!

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



Executing commands which require quotes and variables is practically impossible: Microsoft Connect

This is an interesting forum thread about what I’ve spent the last 2 hours researching:  How to properly quote commands with parameters and interpolated paths so that they will execute as a 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 same thing in powershell and this is what you get:


PS C:\Documents and Settings\nunya> “C:\Program Files\Mozilla Firefox\firefox.exe” -CreatProfile myTest
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
Advertisements
  1. March 30, 2012 at 11:15 am

    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
      March 31, 2012 at 5:23 pm

      @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

  2. April 2, 2012 at 2:37 pm

    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
      April 2, 2012 at 4:12 pm

      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 care.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: