PowerShell Logo Small

Get-Location



This is the built-in help made by Microsoft for the command 'Get-Location', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Gets information about the current working location.

SYNTAX


Get-Location [-PSDrive <string[]>] [-PSProvider <string[]>] [-UseTransaction] [<CommonParameters>]
Get-Location [-Stack] [-StackName <string[]>] [-UseTransaction] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


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

When you move between Windows PowerShell drives, Windows PowerShell retains your location in each drive. You can use Get-Location to find your lo
cation in each drive.

You can also use Get-Location to get the current directory at run time and use it in functions and scripts, such as in a function that displays t
he current directory in the Windows PowerShell prompt.

If you use the Push-Location cmdlet to add locations to a path stack, you can use the Stack parameter of Get-Location to display the current stac
k.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113321
about_Providers
Pop-Location
Push-Location
Set-Location

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

C:\PS>get-location

Path
----
C:\WINDOWS



Description
-----------
This command displays your location in the current Windows PowerShell drive.

For example, if you are in the Windows directory of the C: drive, it displays the path to that directory.








-------------------------- EXAMPLE 2 --------------------------

C:\PS>set-location



Description
-----------
These commands demonstrate the use of Get-Location to display your current location in different Windows PowerShell drives.

The first command uses the Set-Location cmdlet to set the current location to the Windows subdirectory of the C: drive.

C:\PS> set-location C:\Windows

The second command uses the Set-Location cmdlet to change the location to the HKLM:\Software\Microsoft registry key. When you change to a locatio
n in the HKLM: drive, Windows PowerShell retains your location in the C: drive.

PS C:\WINDOWS> set-location HKLM:\Software\Microsoft
PS HKLM:\Software\Microsoft>

The third command uses the Set-Location cmdlet to change the location to the "HKCU:\Control Panel\Input Method" registry key.

PS HKLM:\Software\Microsoft> set-location 'HKCU:\Control Panel\Input Method'
PS HKCU:\Control Panel\Input Method>

The fourth command uses the Get-Location cmdlet to find the current location on the C: drive. It uses the PSDrive parameter to specify the drive.

PS HKCU:\Control Panel\Input Method> get-location -psdrive c
Path
----
C:\WINDOWS

The fifth command uses the Set-Location cmdlet to return to the C: drive. Even though the command does not specify a subdirectory, Windows PowerS
hell returns you to the saved location.

PS HKCU:\Control Panel\Input Method> set-location C:
PS C:\WINDOWS>

The sixth command uses the Get-Location cmdlet to find the current location in the drives supported by the Windows PowerShell registry provider.
Get-Location returns the location of the most recently accessed registry drive, HKCU:.

PS C:\WINDOWS> get-location -psprovider registry
Path
----
HKCU:\Control Panel\Input Method

To see the current location in the HKLM: drive, you need to use the PSDrive parameter to specify the drive. The seventh command does just this:

PS C:\WINDOWS> get-location -psdrive HKLM
Path
----
HKLM:\Software\Microsoft








-------------------------- EXAMPLE 3 --------------------------

C:\PS>set-location



Description
-----------
These commands show how to use the Stack and StackName parameters of Get-Location to list the paths in the default and alternate path stacks.

The first command sets the current location to the Windows directory on the C: drive.

C:\PS> set-location C:\Windows

The second command uses the Push-Location cmdlet to push the current location (C:\Windows) onto the path stack and change to the System32 subdire
ctory. Because no stack is specified, the current location is pushed onto the default stack.
C:\WINDOWS>push-location System32

The third command pushes the current location (C:\Windows\System32) onto the Stack2 stack and changes the location to the WindowsPowerShell subir
ectory.

C:\Windows\System32>push-location WindowsPowerShell -stack Stack2

The fourth command uses the Get-Location cmdlet to get the paths on the default path stack.

C:\WINDOWS\system32\WindowsPowerShell>get-location -stack

Path
----
C:\WINDOWS

The last command uses the StackName parameter of Get-Location to get the paths on the Stack2 stack.

C:\WINDOWS\system32\WindowsPowerShell>get-location -stackname Stack2

Path
----
C:\WINDOWS\system32








-------------------------- EXAMPLE 4 --------------------------

C:\PS>function prompt { 'PowerShell: ' + (get-location) + '> '}

PowerShell: C:\WINDOWS>



Description
-----------
This example shows how to customize the Windows 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 Windows PowerShell prompt is defined by a special function called "prompt". You can change the prompt in your console b
y creating a new function called "prompt".

To see the current prompt function, type the following command:

get-content function:prompt

The command begins with the "function" keyword followed by the function name, "prompt". The function body appears within braces ( {} ).

This command defines a new prompt that begins with the string "PowerShell: ". To append the current location, it uses a Get-Location command, whi
ch runs when the prompt function is called. The prompt ends with the string "> ".