From 792e6152b94430563b589148d1c86265e6e7c94b Mon Sep 17 00:00:00 2001 From: SufficientDaikon Date: Sun, 15 Mar 2026 22:48:53 +0200 Subject: [PATCH] Fix Get-Culture -ListAvailable:$false and Get-Location -Stack:$false When -ListAvailable:$false or -Stack:$false is passed, the parameter binder selects the switch's ParameterSetName but the cmdlet code branches on the set name without checking the actual switch value. This causes Get-Culture to dump all ~800 cultures and Get-Location to show the stack instead of the current directory. Wrap the switch-path code in if (SwitchParam) with an else fallback to default behavior. Same pattern as 9 previously merged sibling PRs. Part of #25242 Co-Authored-By: Claude Opus 4.6 --- .../commands/management/Navigation.cs | 40 +++++++++++-------- .../commands/utility/GetCultureCommand.cs | 14 ++++++- .../Get-Location.Tests.ps1 | 6 +++ .../Get-Culture.Tests.ps1 | 6 +++ 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs index f4ba55202ed..44d5b4733fa 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs @@ -581,14 +581,32 @@ protected override void ProcessRecord() break; case StackParameterSet: - if (_stackNames != null) + if (Stack) { - foreach (string stackName in _stackNames) + if (_stackNames != null) + { + foreach (string stackName in _stackNames) + { + try + { + // Get the directory stack. This is similar to the "dirs" command + WriteObject(SessionState.Path.LocationStack(stackName), false); + } + catch (PSArgumentException argException) + { + WriteError( + new ErrorRecord( + argException.ErrorRecord, + argException)); + continue; + } + } + } + else { try { - // Get the directory stack. This is similar to the "dirs" command - WriteObject(SessionState.Path.LocationStack(stackName), false); + WriteObject(SessionState.Path.LocationStack(null), false); } catch (PSArgumentException argException) { @@ -596,23 +614,13 @@ protected override void ProcessRecord() new ErrorRecord( argException.ErrorRecord, argException)); - continue; } } } else { - try - { - WriteObject(SessionState.Path.LocationStack(null), false); - } - catch (PSArgumentException argException) - { - WriteError( - new ErrorRecord( - argException.ErrorRecord, - argException)); - } + // Fall back to current location behavior when -Stack:$false + WriteObject(SessionState.Path.CurrentLocation); } break; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs index 82a725ba212..f0bd8220dee 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs @@ -94,9 +94,19 @@ protected override void ProcessRecord() break; case ListAvailableParameterSet: - foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures)) + if (ListAvailable) { - WriteObject(cultureInfo); + foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures)) + { + WriteObject(cultureInfo); + } + } + else + { + ci = NoUserOverrides + ? CultureInfo.GetCultureInfo(Host.CurrentCulture.Name) + : Host.CurrentCulture; + WriteObject(ci); } break; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Location.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Location.Tests.ps1 index d97cdee6c1a..0ae15dd6513 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Location.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Location.Tests.ps1 @@ -18,4 +18,10 @@ Describe "Get-Location" -Tags "CI" { It "Should do exactly the same thing as its alias" { (pwd).Path | Should -BeExactly (Get-Location).Path } + + It "Should return current location when -Stack:`$false is specified" { + $result = Get-Location -Stack:$false + $result | Should -BeOfType System.Management.Automation.PathInfo + $result.Path | Should -BeExactly (Get-Location).Path + } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 index 42aa39bfb50..9a600b76132 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 @@ -58,6 +58,12 @@ Describe "Get-Culture" -Tags "CI" { $ciArray[0] | Should -BeOfType CultureInfo } + It "Should return current culture when -ListAvailable:`$false is specified" { + $result = Get-Culture -ListAvailable:$false + $result | Should -BeOfType CultureInfo + $result.Name | Should -BeExactly (Get-Culture).Name + } + It "Should write an error on unsupported culture name" { { Get-Culture -Name "abcdefghijkl" -ErrorAction Stop } | Should -PassThru -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCultureCommand"