Push-Location
Push-Location [-LiteralPath <String>] [-PassThru] [-StackName <String>] [-UseTransaction] [<CommonParameters>]
Push-Location [[-Path] <String>] [-PassThru] [-StackName <String>] [-UseTransaction] [<CommonParameters>]
The Push-Location cmdlet adds, or pushes, the current location onto a location stack. If you specify a path, this cmdlet pushes the current location onto a location stack and then changes the current location to the location specified by the path. You can use the Pop-Location cmdlet to get locations from the location stack.
By default, Push-Location pushes the current location onto the current location stack, but you can use the StackName parameter to specify another location stack. If the stack does not exist, Push-Location creates it.
For more information about location stacks, see the Notes.
Parameters
-LiteralPath <String>
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies the path of the new location. Unlike the Path parameter, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcard characters. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.
-PassThru [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Passes an object that represents the location to the pipeline. By default, this cmdlet does not generate any output.
-Path <String>
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies the path of the new location. This cmdlet your location to the location specified by this path after it adds, or pushes, the current location onto the top of the stack. Enter a path of any location whose provider supports this cmdlet. Wildcard characters are permitted.
-StackName <String>
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies the location stack to which the current location is added. Enter a location stack name. If the stack does not exist, Push-Location creates it.
Without this parameter, Push-Location adds the location to the current location stack. By default, the current location stack is the unnamed default location stack that Windows PowerShell creates. To make a location stack the current location stack, use the StackName parameter of the Set-Location cmdlet.
-UseTransaction [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Inputs
System.StringOutputs
None, System.Management.Automation.PathInfoExamples
-
Change location:
PS C:\> Push-Location -Path "C:\Windows"
This command pushes the current location onto the default location stack and then changes the location to C:\Windows.
-
Change location in a named stack:
PS C:\> Push-Location -Path "HKLM:\Software\Policies" -StackName RegFunction
This command pushes the current location onto the RegFunction stack and changes the current location to the HKLM:\Software\Policies location. You can use the Location cmdlets in any Windows PowerShell drive (PSDrive).
-
Push the current location onto the default stack:
PS C:\> Push-Location
This command pushes the current location onto the default stack. It does not change the location.
-
Create and use a named stack:
PS C:\> Push-Location ~ -StackName "Stack2" PS C:\Users\User01> Pop-Location -StackName "Stack2" PS C:\>
These commands show how to create and use a named location stack.
The first command pushes the current location onto a new stack named Stack2, and then changes the current location to the root directory (%USERPROFILE%), which is represented in the command by the tilde symbol (~) or $home. If Stack2 does not already occur in the session, Push-Location creates it.
The second command uses Pop-Location to pop the original location (PS C:\>) from the Stack2 stack. Without StackName , Pop-Location would pop the location from the unnamed default stack.
Additional Notes
* A stack is a last-in, first-out list in which only the most recently added item can be accessed. You add items to a stack in the order that you use them, and then retrieve them for use in the reverse order. Windows PowerShell lets you store provider locations in location stacks. Windows PowerShell creates an unnamed default location stack and you can create multiple named location stacks. If you do not specify a stack name, Windows PowerShell uses the current location stack. By default, the unnamed default location is the current location stack, but you can use Set-Location * to change the current location stack. To manage location stacks, use the Windows PowerShell Location * cmdlets, as follows: - To add a location to a location stack, use the Push-Location cmdlet. - To get a location from a location stack, use the Pop-Location cmdlet. - To display the locations in the current location stack, use the Stack parameter of the Get-Location cmdlet. - To display the locations in a named location stack, use the StackName parameter of the Get-Location cmdlet. - To create a new location stack, use the StackName parameter of the Push-Location cmdlet. If you specify a stack that does not exist, Push-Location creates the stack. - To make a location stack the current location stack, use the StackName parameter of the Set-Location cmdlet. The unnamed default location stack is fully available only when it is the current location stack. If you make a named location stack the current location stack, you can no longer use Push-Location or Pop-Location cmdlets add or get items from the default stack or use a Get-Location command to display the locations in the unnamed stack. To make the unnamed stack the current stack, use the StackName parameter of Set-Location * with a value of $Null or an empty string (""). You can also refer to Push-Location by its built-in alias, pushd *. For more information, see about_Aliases. Push-Location * is designed to work with the data exposed by any provider. To list the providers available in your session, type `Get-PSProvider`. For more information, see about_Providers.
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands