0

Hi everyone I am trying to fetch the version of Microsoft Visual Studio components from powershell I tried "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -property catalog_productDisplayVersion command But its working only on 'command Prompt'

I ran this command, "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -property catalog_productDisplayVersion

output: At line:1 char:72

  • ... x86)\Microsoft Visual Studio\Installer\vswhere.exe" -property catalog ...
  •                                                     ~~~~~~~~~
    

Unexpected token '-property' in expression or statement. At line:1 char:82

  • ... Studio\Installer\vswhere.exe" -property catalog_productDisplayVersion
  •                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

Unexpected token 'catalog_productDisplayVersion' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

Compo
  • 36,585
  • 5
  • 27
  • 39
ashish
  • 5
  • 4
  • I have removed some of your tags, as this question is specifically about an error resulting only when running in PowerShell. Also, does [this](https://stackoverflow.com/q/72439724) help/ – Compo Feb 22 '23 at 20:02
  • In short: An executable path that is _quoted_ or contains _variable references_ must - for syntactic reasons - be invoked with `&`, the [call operator](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#call-operator-); see the linked duplicate for details. – mklement0 Feb 22 '23 at 20:37

1 Answers1

1

Prefix the command with the PowerShell "Call Operator", which is an ampersand:

& "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -property catalog_productDisplayVersion

It prevents PowerShell from trying to parse the string.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • Good point re `&`, but let me clarify, since "trying to parse the string" is a bit ambiguous: It's about whether a statement is parsed as a _command_ (argument mode) or an _expression_ (expression mode), as discussed in the [about_Parsing](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Parsing) help topic. A _quoted_ token at the start of a statement is interpreted as a _string literal_ starting an _expression_. Starting a statement with `&` forces interpretation as a _command_, ensuring that the quoted string is interpreted as a command/executable. – mklement0 Feb 22 '23 at 20:49
  • How to use call(&) operator in batch script, – ashish Feb 22 '23 at 21:13
  • @ashish, `cmd.exe`'s batch language uses `&` exclusively for _separating statements_ (PowerShell uses `;`). Unlike PowerShell, it requires _no_ operator in order to invoke executables whose names / paths are quoted; e.g., `"where.exe" findstr` works just fine. – mklement0 Feb 23 '23 at 02:32