diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 7cf4e0976d7..a7931e7d4cf 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -316,6 +316,14 @@ internal bool ServerMode } } + internal bool ShowVersion + { + get + { + return _showVersion; + } + } + internal Serialization.DataFormat OutputFormat { get @@ -500,7 +508,6 @@ private void ParseHelper(string[] args) // chop off the first character so that we're agnostic wrt specifying / or - // in front of the switch name. - switchKey = switchKey.Substring(1); // chop off the second dash so we're agnostic wrt specifying - or -- @@ -509,6 +516,18 @@ private void ParseHelper(string[] args) switchKey = switchKey.Substring(1); } + // If version is in the commandline, don't continue to look at any other parameters + if (MatchSwitch(switchKey, "version", "v")) + { + _showVersion = true; + _showBanner = false; + _noInteractive = true; + _skipUserInit = true; + _noExit = false; + break; + } + + if (MatchSwitch(switchKey, "help", "h") || MatchSwitch(switchKey, "?", "?")) { _showHelp = true; @@ -1091,6 +1110,7 @@ private bool CollectArgs(string[] args, ref int i) private bool _serverMode; private bool _namedPipeServerMode; private bool _sshServerMode; + private bool _showVersion; private string _configurationName; private PSHostUserInterface _hostUI; private bool _showHelp; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index f60ab7f222a..0d780dfabbe 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -190,6 +190,15 @@ internal static int Start( s_cpp.Parse(tempArgs); + if (s_cpp.ShowVersion) + { + // Alternatively, we could call s_theConsoleHost.UI.WriteLine(s_theConsoleHost.Version.ToString()); + // or start up the engine and retrieve the information via $psversiontable.GitCommitId + // but this returns the semantic version and avoids executing a script + s_theConsoleHost.UI.WriteLine("powershell " + PSVersionInfo.GitCommitId); + return 0; + } + // Servermode parameter validation check. if ((s_cpp.ServerMode && s_cpp.NamedPipeServerMode) || (s_cpp.ServerMode && s_cpp.SocketServerMode) || (s_cpp.NamedPipeServerMode && s_cpp.SocketServerMode)) { diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 098b52f937c..be711e0c48c 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -153,6 +153,14 @@ internal static Version PSVersion } } + internal static string GitCommitId + { + get + { + return (string)GetPSVersionTable()["GitCommitId"]; + } + } + internal static Version CLRVersion { get diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index cbb5b08b0cc..531187fc77e 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -164,6 +164,18 @@ Describe "ConsoleHost unit tests" -tags "Feature" { $actual | Should Be $expected } + It "-Version should return the engine version" { + $currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString() + $observed = & $powershell -version + $observed | should be $currentVersion + } + + It "-Version should ignore other parameters" { + $currentVersion = "powershell " + $PSVersionTable.GitCommitId.ToString() + $observed = & $powershell -version -command get-date + # no extraneous output + $observed | should be $currentVersion + } } Context "Pipe to/from powershell" {