-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Description
Get-Location -Stack currently outputs a [System.Management.Automation.PathInfoStack] stack object rather than an array of [System.Management.Automation.PathInfo] instances representing the individual locations in the stack.
While this is behavior is documented, and changing it would technically be a breaking change, it should be considered for two reasons:
-
Outputting a custom stack object rather than an array of locations is of little benefit, given that location stacks are meant to be managed via the
*-Locationcmdlets.-
Stack management support in these cmdlets currently lacks options for deleting / clearing a stack, but that is a separate issue - see Managing location stacks via the *-Location cmdlets lacks ability to clear stacks #4643.
-
As an aside: While calling the
.Clear()method on the stack object may seem like a workaround for the current inability to clear a stack using cmdlets, it doesn't actually work (only clears the object returned, not the actual stack).
-
-
Outputting a custom stack object rather than an array of locations contravenes the expectation that a regular PowerShell array is output that can be indexed into, which doesn't work as intended with the stack object (
.ToArray()[0]must be used, for instance).- The current default output certainly looks like a collection (array) of
[System.Management.Automation.PathInfo]instances.
- The current default output certainly looks like a collection (array) of
Current behavior
# Try to obtain the 2nd location from the bottom of the stack, which is "/"
> Push-Location /; Push-Location $HOME; (Get-Location -Stack)[-2]
# !! No outputBecause Get-Location -Stack returns a scalar - the stack object - indexing into the scalar with [-2] makes PowerShell return $null.
To make this work currently, .ToArray() must be used:
# Note the awkward .ToArray()
> Push-Location /; Push-Location $HOME; (Get-Location -Stack).ToArray()[-2]
Path
----
/
Desired behavior
If a regular array is returned, indexing will work as expected.
> Push-Location /; Push-Location $HOME; (Get-Location -Stack)[-2]
Path
----
/
Environment data
PowerShell Core v6.0.0-beta.5 on macOS 10.12.6
PowerShell Core v6.0.0-beta.5 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.5 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.483 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)