PowerShell Get-Location And Set-Location Cmdlets
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Get-ChildItem cmdlet in PowerShell.
https://cloudaffaire.com/powershell-get-childitem-cmdlet/
In this blog post, we will discuss Get-Location and Set-Location cmdlets in PowerShell. You can use Get-Location and Set-Location cmdlets in PowerShell to get and set the current working directory. Get-Location cmdlet is equivalent to ‘pwd’ command and Set-Location cmdlet is equivalent to ‘cd’ command. You can also use Get-Location and Set-Location cmdlet to get and set location stack. A stack is a last-in, first-out list in which only the most recently added item is accessible. You add items to a stack in the order that you use them, and then retrieve them for use in the reverse order. PowerShell lets you store provider locations in location stacks. PowerShell creates an unnamed default location stack and you can create multiple named location stacks.
Get-Location And Set-Location Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 |
## Get-Location ## [-PSProvider ## [-PSDrive ## [-UseTransaction] ## [ ## ## Set-Location ## [[-Path] ## [-PassThru] ## [-UseTransaction] ## [ |
Get-Location Cmdlet Argument List:
- –PSDrive: Gets the current location in the specified PowerShell drive.
- –PSProvider: Gets the current location in the drive supported by the specified PowerShell provider.
- –Stack: Indicates that this cmdlet displays the locations added to the current location stack.
- –StackName: Specifies, as a string array, the named location stacks.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
Set-Location Cmdlet Argument List:
- –LiteralPath: Specifies a path of the location. The value of the LiteralPath parameter is used exactly as it is typed.
- –PassThru: Returns a PathInfo object that represents the location. By default, this cmdlet does not generate any output.
- –Path: Specify the path of a new working location. If no path is provided, Set-Location defaults to the current user’s home directory.
- –StackName: Specifies the existing location stack name that this cmdlet makes the current location stack.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
PowerShell Get-Location And Set-Location Cmdlet:
Get Current Working Directory In PowerShell:
1 2 3 4 5 6 7 8 9 |
####################################################### ## PowerShell | Cmdlet | Get-Location | Set-Location ## ####################################################### ## PowerShell Latest Version (5) ## get current working directory in PowerShell Get-Location #pwd |
Set Current Working Directory In PowerShell:
1 2 3 4 |
## set current working directory in PowerShell Set-Location -Path C:\ #cd C:\ |
Set Current Working Directory To Registry In PowerShell:
1 2 3 4 5 6 7 8 9 10 |
## navigate through registry in PowerShell Set-Location -Path HKCU:\Software\Microsoft Get-Location Set-Location -Path HKCU:\Software\Microsoft\Notepad Get-Location Set-Location -Path C:\ Get-Location |
Create And Manage Location Stack In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## create and manage location stack in PowerShell Push-Location C:\Windows -StackName MyStack Get-Location -StackName MyStack Push-Location C:\Windows\System32 -StackName MyStack Get-Location -StackName MyStack Push-Location C:\Windows\System32\drivers -StackName MyStack Get-Location -StackName MyStack Pop-Location -StackName MyStack Get-Location -StackName MyStack Pop-Location -StackName MyStack Get-Location -StackName MyStack Pop-Location -StackName MyStack Get-Location |
Hope you have enjoyed this article. In the next blog post, we will discuss Test-Path cmdlet in PowerShell.
To Set more details on PowerShell, kindly follow below official documentation