Updated March 6, 2023
Definition of PowerShell Location
PowerShell locations cmdlets Get-Location, Set-Location, Push-Location, and Pop-Location are used to get the current location of the PowerShell console, set the location to the directory, registry, or certificate path, move to the new location, and retain the previous location using stack and script uses these cmdlets to set the location of the source folder so that the all the source files reside in the folder can be easily executed.
Syntax:
Get-Location Syntax:
Get-Location
[-PSProvider <String[]>]
[-PSDrive <String[]>]
[<CommonParameters>]
Get-Location
[-Stack]
[-StackName <String[]>]
[<CommonParameters>]
Set-Location Syntax:
Set-Location
[[-Path] <String>]
[-PassThru]
[<CommonParameters>]
Set-Location
-LiteralPath <String>
[-PassThru]
[<CommonParameters>]
Set-Location
[-PassThru]
[-StackName <String>]
[<CommonParameters>]
Push-Location Syntax:
Push-Location
[[-Path] <String>]
[-PassThru]
[-StackName <String>]
[<CommonParameters>]
Push-Location
[-LiteralPath <String>]
[-PassThru]
[-StackName <String>]
[<CommonParameters>]
Pop-Location Syntax:
Pop-Location
[-PassThru]
[-StackName <String>]
[<CommonParameters>]
In the above all the multiple syntaxes we can use only one set at a time for the particular cmdlet. For example, In the Push-Location cmdlet, we can either use -Path or -LiteralPath but can’t use both together.
How does the location in PowerShell Works?
There are other location cmdlets as well in PowerShell with different modules but PowerShell by default ships with the 4 cmdlets for location (Push-Location, Pop-Location, Set-Location, Get-Location) and they are from the PowerShell module Microsoft.PowerShell.Management.
We can check the mentioned cmdlets with the command,
Get-Command -Name *location* -Module Microsoft.PowerShell.Management
Output:
When we use the Get-Location command, it shows the path of the current location.
PS C:\Temp> Get-Location
Output:
With the Set-Location command, we can change the directory to another directory, registry, or certificate store location. This command is the alias of the cmd command “cd” which means you can use both commands alternatively.
When we use the Set-Location, we can push the current location to the new stack so later it can be retrieved. The below command will change the location to the registry.
PS C:\Temp\Temp1> Set-Location HKLM:\HARDWARE\
Output:
Push-Location and the Pop-Location commands work a little differently than the Set-Location and Get-Location. The Push-Location command pushes the current location to the stack and then changes to the current location while the Pop-Location command is used to retrieve the pushed location from the stack as shown below.
PS C:\Temp> Push-Location -Path C:\Test1\
Output:
When we check the stack, it should show the C:\Temp.
PS C:\Test1> Get-Location -Stack
Output:
Let’s push the other location and check the stack.
PS C:\Test1> Push-Location -Path C:\Test2\
PS C:\Test2> Get-Location -Stack
Output:
When we use the Pop-Location command, it will retrieve the stored output in the LIFO (Last In First Out) order.
PS C:\Test2> Pop-Location
PS C:\Test1> Pop-Location
Output:
Examples
Example #1: Setting Environment variable location.
We can use the Set-Location command to switch the directory to the environment variable location. Below we are using the windows root directory environment variable.
PS C:\> Set-Location $env:windir
Output:
We can also set the environment variable location as a target directory as shown below.
PS C:\Windows> Set-Location Env:\
Output:
Example #2: Setting directory location to registry and certificate path.
We can set the location of the registry as shown below.
PS C:\> Set-Location HKLM:\
Output:
Or the child path of the registry.
PS C:\> Set-Location HKLM:\HARDWARE\
Output:
Similarly, we can set the Certificate manager path as well.
PS C:\> Set-Location Cert:\LocalMachine\
Output:
Example #3: Getting the script execution location.
To get the location of the script that is executing we can use the MyInvocation command.
MyCommand shows the name of the script and InvocationName shows the path of the script.
$MyInvocation.InvocationName
Example #4: Getting PowerShellModule path location.
A PowerShell module path is stored at the location $env: PsModulePath
You can use the below command to get the PS nodule path.
$env:PSModulePath -split ';'
Output:
Example #5: Using the Push-Location and Pop-Location command in a script.
Suppose we have a script that uses a Push-Location command to get the data from the particular location and then uses a Pop-Location command to get back to the original location so that the script can resume execution with the reference to the old path.
Write-Output "Current Directory Location: $(Get-Location)"
Write-Output "Retrieve the files/folders from the C:\temp"
Push-Location C:\Test1
dir
Write-Output "Returning to the original location"
Pop-Location
Write-Output "Lastest Location: $(Get-Location)"
Output:
Example #6: Push the locations to the new name stack.
We can use the different name stack to push the location to the new stack so when we are working on the specific script, we don’t need to go our locations into the default name stack.
Push-Location C:\Windows\System32\drivers\etc\ -StackName 'MyStack' -PassThru
Output:
We can retrieve the previous location from the specific name stack using the below command.
Pop-Location -StackName 'MyStack'
Output:
Example #7: Setting the location using another stack.
Here, we can set the location using a different name stack. We will first create the new stack by pushing the location to the new stack and set the location of that stack and then we will get back to our previous location.
Push-Location $env:windir\System32\drivers\etc\ -StackName 'MyStack' -PassThru
Set-Location -StackName 'MyStack'
The above commands push the location to the stack and the Set-Location sets that specific location from the stack.
Output:
We can check the locations stored inside the stack using,
Get-Location -StackName 'MyStack'
Output:
Example #8: Navigate the location using ‘+’ or ‘-‘.
We can navigate the location using the ‘+’ and ‘-‘ sign in the Set-Location or the cd command.
PS C:\> Set-Location $env:CommonProgramFiles
PS C:\Program Files\Common Files> Set-Location HKLM:\HARDWARE\
PS HKLM:\HARDWARE\> Set-Location Cert:\CurrentUser\
We have navigated the above folders now. We will use the ‘-‘ and ‘+’ sign to move to the previous path and the next path.
Set-Location -Path '-'
Set-Location -Path '+'
Output:
Conclusion
PowerShell location commands are very much helpful inside the script for setting up the path of the directory for the source files and return to the original path so that human intervention is not needed when a script needs the desired location to execute dependent files.
Recommended Articles
This is a Guide to PowerShell Location. Here we discuss Introduction, syntax, and parameters, examples with code implementation. You may also have a look at the following articles to learn more –