EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Location

PowerShell Location

Updated March 6, 2023

PowerShell Location

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

PowerShell Location 1

When we use the Get-Location command, it shows the path of the current location.

PS C:\Temp> Get-Location

Output:

PowerShell Location 2

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:

PowerShell Location 3

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:

PowerShell Location 4

When we check the stack, it should show the C:\Temp.

PS C:\Test1> Get-Location -Stack

Output:

PowerShell Location 5

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:

PowerShell Location 6

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:

pop location

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:

setting enivorment

We can also set the environment variable location as a target directory as shown below.

PS C:\Windows> Set-Location Env:\

Output:

set location

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:

example 2

Or the child path of the registry.

PS C:\> Set-Location HKLM:\HARDWARE\

Output:

set location 1

Similarly, we can set the Certificate manager path as well.

PS C:\> Set-Location Cert:\LocalMachine\

Output:

local machine

Example #3: Getting the script execution location.

To get the location of the script that is executing we can use the MyInvocation command.

Mycommand

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 4

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:

test 1

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:

example 6

We can retrieve the previous location from the specific name stack using the below command.

Pop-Location -StackName 'MyStack'

Output:

mystack

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:

example 7

We can check the locations stored inside the stack using,

Get-Location -StackName 'MyStack'

Output:

C Temp

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:

example 8

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 –

  1. PowerShell join string
  2. PowerShell Exit
  3. PowerShell Wait
  4. PowerShell Like Operator
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle2000+ Hour of HD Videos | 80 Learning Paths | 400+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more