Get-Location PowerShell Command

Get-Location | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Location.

When to use Get-Location?

The Get-Location cmdlet gets an object that represents the current directory, much like the print working directory (pwd) command.

When you move between PowerShell drives, PowerShell retains your location in each drive. You can use this cmdlet to find your location in each drive.

You can use this cmdlet to get the current directory at run time and use it in functions and scripts, such as in a function that displays the current directory in the PowerShell prompt.

NOTE: You can also use this cmdlet to display the locations in a location stack.

What version of PowerShell am I using?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-Location?

Display your current drive location:

Get-Location

This command displays your location in the current PowerShell drive.

Display your current location for different drives:

This example demonstrates the use of Get-Location to display your current location in different PowerShell drives. Set-Location is used to change the location to several different paths on different PSDrives.

Set-Location C:\Windows
Set-Location HKLM:\Software\Microsoft
Set-Location “HKCU:\Control Panel\Input Method”
Get-Location -PSDrive C
Get-Location -PSDrive HKLM
Get-Location -PSProvider Registry

Get locations using stacks:

This example shows how to use the –Stack and –StackName parameters of Get-Location to list the locations in the current location stack and alternate location stacks.

The Push-Location cmdlet is used to change into two different locations.

The second push uses a different stack name. The –Stack parameter of Get-Location displays the contents of the default stack. The –StackName parameter of Get-Location displays the contents of the stack named Stack2.

Push-Location System32
Push-Location WindowsPowerShell -StackName Stack2
Get-Location -Stack
Get-Location -StackName Stack2

Customize the PowerShell prompt:

The function that defines the prompt includes a Get-Location command, which is run whenever the prompt appears in the console.

The format of the default PowerShell prompt is defined by a special function named prompt.

You can change the prompt in your console by creating a new function named prompt.

To see the current prompt function, type the following command: Get-Content Function:\prompt

function prompt { ‘PowerShell: ‘ + (Get-Location) + ‘> ‘}

Learn last week’s command: Get-ItemPropertyValue.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.