PowerShell Get-Location

The Get-Location cmdlet gets the information about the current working directory or a location stack.

This cmdlet gets an object which represents the current directory, much like the print present working directory (pwd) command. When we switch between the drives, PowerShell retains our location in each drive. We can also use this cmdlet to find the location in each drive.

This cmdlet is used to get the current directory at execution time and use it in scripts and functions, such as in functions that display the current working directory in the PowerShell prompt. We can also use Get-Location cmdlet to view the location in the location stack.

Syntax

Parameters

-PSDrive

The -PSDrive parameter is used to specify the current location in the given PowerShell drive that this cmdlet gets in the operation.

For instance, if any user in a Certificate: drive, using this parameter, then he/she can find its current location in the C: drive.

-PSProvider

The -PSProvider parameter is used to specify the current location in a drive supported by the PowerShell provider that this cmdlet gets in the operation. If the specified PowerShell provider supports more than one drive, then Get-Location cmdlet returns a location on the most recently accessed drive.

For example, if any user in C: drive using this parameter, then he/she can find its current location in the drives of PowerShellRegistry provider.

-Stack

The -Stack parameter is used to display the locations in the current location stack. If a user wants to display the locations in the different location stack, he can use the StackName parameter.

-StackName

The -StackName parameter is used to display the locations in the different location stacks.

Get-Location cmdlet cannot display the locations in default stack unless it is the current stack.

Examples

Example1: To display a current drive location

PowerShell Get-Location

The cmdlet in this example displays a location in the current PowerShell drive. Suppose a user is in the Windows directory of D: drive, this cmdlet displays the path to that directory.< /p>

Example2: To display a current location for different drives:

This example explains the use of get-location to display the current location in different PowerShell drives.

i. The first cmdlet in this example uses the set-location cmdlet to set the current location to the Windows directory of C: drive.

PowerShell Get-Location

ii. The second cmdlet in this example uses the set-location cmdlet to change the location to the HKLM: Software Microsoft registry key. When we change a location in the HKLM: drive, Windows PowerShell retains our location in C:drive.

PowerShell Get-Location

iii. The third cmdlet in this example uses the set-location cmdlet to change a location to the HKCU:Control PanelInput method registry key.

PowerShell Get-Location

iv. The fourth cmdlet in this example uses the Get-location cmdlet to find the current location on a C: drive. This cmdlet uses the -PSDrive parameter to specify the drive.

PowerShell Get-Location

v. The fifth cmdlet in this example uses the Set-location cmdlet to return to the C:drive.

PowerShell Get-Location

vi. The sixth cmdlet in this example uses the Get-Location cmdlet to find a current location in the drives supported by the PowerShell registry provider. In this, Get-location returns a location of recently accessed registry drive, HKCU.

PowerShell Get-Location

vii. The seventh cmdlet in this example uses the -PSDrive parameter to see the current location in HKLM: drive.

PowerShell Get-Location

Example3: To customize the PowerShell prompt

PowerShell Get-Location

This example displays how to customize the Windows PowerShell prompt. A function that defines the prompt includes a Get-location cmdlet, which is executed whenever a prompt appears in the console.

This cmdlet begins with the Function keyword followed by the name of a function, which is prompt. The body of a function appears in the braces ‘{ }’.

This cmdlet defines a new prompt that begins with the ‘PowerShell:‘ string. To add the current location, it uses a get-location cmdlet, which executes when the prompt function is called. The prompt function ends with the string ‘>’.

Type this command, Get-Content Function:prompt to see the current prompt function.