Most of the Applications use the Current Working Directory to store the Application related files. PowerShell provides set of cmdlets, useful to manage the current working directory. In PowerShell, we use the noun “Location” for the working directory; so the same used for the cmdlets.
The Get-Location
cmdlet
This command is useful to get the current location (current working directory). Alias name for this is pwd
.
PS C:\PowerShell> Get-Location Path ---- C:\PowerShell
The Set-Location
cmdlet
When you write the scripts, there are situations to set the current working directory to your preferred location instead of the currently setting the location. Then this cmdlet is useful to set the current working directory. Alias name for this is cd
.
PS C:\PowerShell> Set-Location C:\ PS C:\> Get-Location Path ---- C:\
Save and Recall the Location Information
Some times it is required to store the location information before changing the location and set to the previous location when required. This can be possible by storing the location information into the variables. But PowerShell provides two commands to deal with storing and restoring the location information. PowerShell internally uses the stack to store and retrieve the information; hence we no need to explicitly store the details in the variables.
The Push-Location
cmdlet
This command is used to push or save the current location into the stack.
The Pop-Location
cmdlet
This command is used to retrieve the last saved location. After calling this, PowerShell will remove the information from the stack.
Here is the complete example to store and recall location information. This demonstrates; storing the information before we change the location and recall the last saved location when required.
PS C:\PowerShell> pwd Path ---- C:\PowerShell PS C:\PowerShell> Push-Location PS C:\PowerShell> Set-Location 'C:\Program Files\Common Files' PS C:\Program Files\Common Files> Get-Location Path ---- C:\Program Files\Common Files PS C:\Program Files\Common Files> Pop-Location PS C:\PowerShell> Get-Location Path ---- C:\PowerShell PS C:\PowerShell>
We will discuss more topics as we go.
[..] David