Skip to content

Fix Get-Culture -ListAvailable:$false and Get-Location -Stack:$false#27034

Open
SufficientDaikon wants to merge 1 commit intoPowerShell:masterfrom
SufficientDaikon:fix/switch-false-culture-location
Open

Fix Get-Culture -ListAvailable:$false and Get-Location -Stack:$false#27034
SufficientDaikon wants to merge 1 commit intoPowerShell:masterfrom
SufficientDaikon:fix/switch-false-culture-location

Conversation

@SufficientDaikon
Copy link

@SufficientDaikon SufficientDaikon commented Mar 15, 2026

PR Summary

Fixes Get-Culture -ListAvailable:$false and Get-Location -Stack:$false, which incorrectly execute the switch-true code path when the switch value is explicitly $false.

Pester breaking pattern

Note

This follows the same fix pattern used in 9 previously merged sibling PRs. Part of the ongoing cleanup tracked in #25242.

The Bug

When -ListAvailable:$false or -Stack:$false is passed, PowerShell's parameter binder selects the corresponding ParameterSetName, but the cmdlet code branches on the set name without checking the actual switch value.

Cmdlet Bug behavior Expected
Get-Culture -ListAvailable:$false Dumps all ~800 cultures Returns current culture
Get-Location -Stack:$false Shows location stack Returns current directory
flowchart TD
    A["-ListAvailable:$false"] --> B["Binder selects\nListAvailableParameterSet"]
    B --> C{"Branches on\nset name only"}
    C -->|"Before · bug"| D["Dumps all 800 cultures ❌"]
    C -->|"After · fix"| E{"if ListAvailable"}
    E -->|true| F["List all cultures"]
    E -->|false| G["Return current culture ✓"]
Loading

The Fix

Wrap the switch-path code in if (SwitchParam) with an else fallback to default behavior:

  case ListAvailableParameterSet:
-     foreach (var ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
-         WriteObject(ci);
+     if (ListAvailable)
+     {
+         foreach (var ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
+             WriteObject(ci);
+     }
+     else
+     {
+         WriteObject(Host.CurrentCulture);
+     }
      break;

What Changed

File Change
GetCultureCommand.cs Guard ListAvailableParameterSet with if (ListAvailable), else return current culture
Navigation.cs Guard StackParameterSet with if (Stack), else return current location
Get-Culture.Tests.ps1 Add -ListAvailable:$false regression test
Get-Location.Tests.ps1 Add -Stack:$false regression test

Tests

Get-Culture Get-Location

  • Get-Culture: 12/12 Pester tests pass (11 existing + 1 new)
  • Get-Location: 2/2 pass (1 existing + 1 new -Stack:$false)
9 sibling PRs already merged (same pattern)
PR Cmdlet Switch
#26140 New-Guid -Empty:$false
#26141 Get-Uptime -Since:$false
#26457 Get-Random -Shuffle:$false
#26460 Get-SecureRandom -Shuffle:$false
#26463 Get-TimeZone -ListAvailable:$false
#26469 New-PSSession -UseWindowsPowerShell:$false
#26474 Split-Path -Qualifier/-NoQualifier/-Leaf/-IsAbsolute:$false
#26479 Test-Connection -Repeat/-MtuSize/-Traceroute:$false
#26485 Where-Object -[Operator]:$false

PR Context

This is part of the ongoing -Switch:$false cleanup tracked in #25242. Nine sibling PRs using the identical fix pattern have already been reviewed and merged. Also open: #26551 covering ConvertTo-Csv / Export-Csv.


PR Checklist

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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant