Skip to content

Commit 792e615

Browse files
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 PowerShell#25242 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 1e5c020 commit 792e615

4 files changed

Lines changed: 48 additions & 18 deletions

File tree

‎src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -581,38 +581,46 @@ protected override void ProcessRecord()
581581
break;
582582

583583
case StackParameterSet:
584-
if (_stackNames != null)
584+
if (Stack)
585585
{
586-
foreach (string stackName in _stackNames)
586+
if (_stackNames != null)
587+
{
588+
foreach (string stackName in _stackNames)
589+
{
590+
try
591+
{
592+
// Get the directory stack. This is similar to the "dirs" command
593+
WriteObject(SessionState.Path.LocationStack(stackName), false);
594+
}
595+
catch (PSArgumentException argException)
596+
{
597+
WriteError(
598+
new ErrorRecord(
599+
argException.ErrorRecord,
600+
argException));
601+
continue;
602+
}
603+
}
604+
}
605+
else
587606
{
588607
try
589608
{
590-
// Get the directory stack. This is similar to the "dirs" command
591-
WriteObject(SessionState.Path.LocationStack(stackName), false);
609+
WriteObject(SessionState.Path.LocationStack(null), false);
592610
}
593611
catch (PSArgumentException argException)
594612
{
595613
WriteError(
596614
new ErrorRecord(
597615
argException.ErrorRecord,
598616
argException));
599-
continue;
600617
}
601618
}
602619
}
603620
else
604621
{
605-
try
606-
{
607-
WriteObject(SessionState.Path.LocationStack(null), false);
608-
}
609-
catch (PSArgumentException argException)
610-
{
611-
WriteError(
612-
new ErrorRecord(
613-
argException.ErrorRecord,
614-
argException));
615-
}
622+
// Fall back to current location behavior when -Stack:$false
623+
WriteObject(SessionState.Path.CurrentLocation);
616624
}
617625

618626
break;

‎src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,19 @@ protected override void ProcessRecord()
9494

9595
break;
9696
case ListAvailableParameterSet:
97-
foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
97+
if (ListAvailable)
9898
{
99-
WriteObject(cultureInfo);
99+
foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
100+
{
101+
WriteObject(cultureInfo);
102+
}
103+
}
104+
else
105+
{
106+
ci = NoUserOverrides
107+
? CultureInfo.GetCultureInfo(Host.CurrentCulture.Name)
108+
: Host.CurrentCulture;
109+
WriteObject(ci);
100110
}
101111

102112
break;

‎test/powershell/Modules/Microsoft.PowerShell.Management/Get-Location.Tests.ps1‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ Describe "Get-Location" -Tags "CI" {
1818
It "Should do exactly the same thing as its alias" {
1919
(pwd).Path | Should -BeExactly (Get-Location).Path
2020
}
21+
22+
It "Should return current location when -Stack:`$false is specified" {
23+
$result = Get-Location -Stack:$false
24+
$result | Should -BeOfType System.Management.Automation.PathInfo
25+
$result.Path | Should -BeExactly (Get-Location).Path
26+
}
2127
}

‎test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Describe "Get-Culture" -Tags "CI" {
5858
$ciArray[0] | Should -BeOfType CultureInfo
5959
}
6060

61+
It "Should return current culture when -ListAvailable:`$false is specified" {
62+
$result = Get-Culture -ListAvailable:$false
63+
$result | Should -BeOfType CultureInfo
64+
$result.Name | Should -BeExactly (Get-Culture).Name
65+
}
66+
6167
It "Should write an error on unsupported culture name" {
6268

6369
{ Get-Culture -Name "abcdefghijkl" -ErrorAction Stop } | Should -PassThru -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCultureCommand"

0 commit comments

Comments
 (0)