Posted on

Taking on PowerShell one cmdlet at a time

Share this post: This is part of an ongoing blog series by Adam Gordon. Adam will show you how to use each PowerShell command each week. Adam will be covering Get-Location this week.

When should you use Get-Location
The Get-Location cmdlet returns an object that represents the current directory. This is similar to the print working directory command (pwd).
PowerShell keeps track of your location when you move between PowerShell drives. This cmdlet can be used to locate your location in each drive.
This cmdlet can be used to get the current directory at the time of execution and to use it in functions or scripts, such a function that displays current directory in PowerShell prompt.
NOTE: This cmdlet can also be used to display locations in a location stack.
What version of PowerShell do I use?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.

How to use Get Location?
Display your current drive location:
This command displays your current location in PowerShell.

Display your current location for different drives:
This example shows how to use Get-Location in PowerShell to display your current location on different PowerShell drives. Set-Location can be used to change the location of several paths on different PSDrives.
Set-Location C:\WindowsSet-Location HKLM:\Software\MicrosoftSet-Location “HKCU:\Control Panel\Input Method”Get-Location -PSDrive CGet-Location -PSDrive HKLMGet-Location -PSProvider Registry

Get locations using stacks:
This example shows you how to use Get-Location’s -StackName and -StackName parameters to list locations in the current and alternate location stacks.
Push-Location cmdlet can be used to change between two locations.
The second push uses a new stack name. The default stack contents are displayed by Get-Location’s -Stack parameter. The contents of the stack named Stack2 are displayed by Get-Location’s -StackName parameter.
Push-Location System32Push-Location WindowsPowerShell -StackName Stack2Get-Location -StackGet-Location -StackName Stack2

Modify the PowerShell prompt
The function that defines the prompt also includes a Get–Location command. This command is executed whenever the prompt appears on the console.
A special function called prompt defines the format of the default PowerShell prompt.
By creating a new function called prompt, you can change the prompt displayed on your console.
To view the prompt function at the moment, type the following command. Get-Content Function:prompt
function prompt ‘PowerShell: ‘ + (Get-Location) + ‘> ‘

Learn last week’s command: Get-ItemPropertyValue.
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.