2

I want to know why when I let spaces between paramters in a powershell command I have errors :

With space : (don't work)

wmic desktopmonitor get screenwidth, screenheight
Expression GET non valide.

Without space : (work)

wmic desktopmonitor get screenwidth,screenheight
ScreenHeight  ScreenWidth

Is that normal ? Because I see a lot of commands on the internet given with spaces!


Update 27/01/2016 Version Detail:

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.0.10240.16384
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   10.0.10240.16384
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3

Both way (with and without space) work with CMD but only the second way (without space) work with PowerShell (see below screen shots):

Cmd.exe

PowerShell

  • 1
    What version of PowerShell are you running and on what version of Windows? These commands are BOTH valid in command prompt (not PowerShell in my setup), but BOTH are invalid in my setup in PowerShell in my environment. More detail of your environment configuration may be helpful. If you have a chance to add a screen shot or a link showing a working and non-working example, that may be helpful as well. – Pimp Juice IT Jan 26 '16 at 22:04
  • $PSVersionTable to find the version you are using. – Michael Frank Jan 26 '16 at 22:35
  • @1Fish_2Fish_RedFish_BlueFish : I wish I answered to your question in my update. – noraj Jan 27 '16 at 20:17
3

Comma is an array operator in PowerShell. So your command:

wmic desktopmonitor get screenwidth, screenheight

have following meaning: invoke wmic with three arguments: string desktopmonitor, string get and array with two strings screenwidth and screenheight. Since wmic is native application, PowerShell have to transform arguments into command line. PowerShell use space as separator when transforming array into command line. So, resulting command line would be following:

wmic desktopmonitor get screenwidth screenheight

You can see that by typing following command:

cmd /c echo wmic desktopmonitor get screenwidth, screenheight

Starting from PowerShell v5 there is one special case here. If array provided directly (not as subexpression) and there is no spaces between commas and array elements, then PowerShell use comma as separator when transforming arrays into command line.

PS> cmd /c echo 1,2,3 (4,5,6) 7,8 ,9
1,2,3 4 5 6 7 8 9

This command:

wmic desktopmonitor get screenwidth,screenheight

qualify for this special case and resulting command line would be following:

wmic desktopmonitor get screenwidth,screenheight

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.