1

I was trying to make use of the suggestion from this question Get NuGet.exe version from command-line to use the PowerShell command

nuget help | select -First 1

This works and outputs just the line with the version on, which is exactly what I want. But when I try to use it in automation it's a problem because I get an error code from the command even thought it succeeds.

PS C:\> $PSVERSIONTABLE

Name                           Value
----                           -----
PSVersion                      5.1.16299.637
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.637
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PS C:\> nuget help | select -First 1
NuGet Version: 4.8.1.5435
PS C:\> $?
False
PS C:\> $LASTEXITCODE
-1

I can't find a reason for the error code in the PowerShell documentation for the select-object command.

Interestingly, the problem is not present in PowerShell Core 6.1

Can anybody shed some light on why I'd be seeing this error code or an alternative for getting the nuget version in PowerShell?

ThetaSinner
  • 399
  • 5
  • 16
  • Possible duplicate of [Difference between $? and $LastExitCode in PowerShell](https://stackoverflow.com/questions/10666035/difference-between-and-lastexitcode-in-powershell) – Tomalak Oct 18 '18 at 08:25
  • 1
    Why not simply use `(Get-Command nuget.exe).Version` or `(Get-Command nuget.exe).FileVersionInfo` resp. it's sub properties? –  Oct 18 '18 at 08:49
  • Thanks for that suggestion @LotPings, that gives me exactly what I need – ThetaSinner Oct 18 '18 at 09:57

0 Answers0