PowerShell change directory is what we call the system navigating the file system using cmdlets. These cmdlets come quite useful when working with files and folders and their relative paths. Various cmdlets are useful to work with current locations, set up the desired location; keep track of the previous location and save the locations in the stack, and even set locations for PSDrives like certificates, the registry, and so on.
General commands for Powershell change Directory
Following are the cmdlets that we use for the syntax to change the directory:
- The cd command to change the path
cd -Path DirectoryPath -PassThru
- The set-location command
set-location -Path DirectoryPath -PassThru
- The push-location command to push the path into the stack.
push-location -Path DirectoryPath -PassThru
- The pop-location command to remove the location from the stack.
pop-location -PassThru # Also to retrieve the same from a specific stack pop-location -StackName stack1 -PassThru
Changing the directory in Powershell
To change the directory in PowerShell, we use many different cmdlets. But before we get into that, let’s first see the different types of paths. One is the absolute path and the other is the relative path. The absolute path is the complete path for the particular directory and the relative path is the direct path.
Take for example, C:\Windows\notepad.exe –> the absolute path
Notepad.exe –> the relative path
To note, an absolute path can be accessed from any directory, while the relative path can only be accessed from the current directory.
Now, that we have fully understood what an absolute path and a relative path are, let’s move forward with the commands which help us change the directory.
The various commands and examples
The set-location command
The set-location command has aliases like (cd, chdir) and we can use this command to change the location of the named directory using the absolute or relative path.
C:\ > Set-Location -Path Cert:\ # To set the registry path for the local machine, use Set-Location -Path HKLM:\ SOFTWARE \
Fastboot OEM Device information !!
The push-location command
There is another way to change the location, and that is using the push-location command. When we specify the push-location command, PowerShell pushes the current location into the stack and then changes the location to the location specified by the path. With the pop-location command, the last location from the stack is used to change the location.
In PowerShell, you can think of the stack as a queue that operates in the order LIFO (Last In First Out). As in,
C:\ > Push location C:\Temp\ C:\Temp >
In the above example, the current location is C: and push-location stores this location in the default stack and navigates to the location C:\temp.
You can check the location stored in the stack with the following command.
When you navigate to another location with the push-location comman
push location $env:SystemRoot # To check the stack location again, use get-location -Stack
When we use the pop location command, the locations are retrieved in sequential order (LIFO). To navigate the location, use the push command with the new stack name, as in:
push location C:\Temp\Data\ -StackName Newstack # To check the location of the newstack get-location -StackName NewStack
# Use the "cd" command to change the location. cd C:\Temp\ #Now, the new location is C:\Temp\ # The above command is similar to cd -path C:\Temp\ -PassThru
You can verify the current location by using the Get-Location command.
Using the pop-location command to change your directory
The pop location command is in use to retrieve the last location from the stack. If the stack name is not there, the default stack comes in use.
C:\Temp\Data > get-location -Stack
If we use the pop-location command, C:\temp is retrieved first, and then C:\ is retrieved in order LIFO.
C:\Temp\Data > Pop-Location C:\Temp > pop location C:\ >
To use the pop-location command for a different stack:
pop-location -StackName Newstack
WRAPPING UP !!
There are several commands to navigate the directory, and we can use them directly in a console or even in a script. They are useful in the script to navigate the path when the script is in execution. Hoping that this tutorial on PowerShell to change your directories. Here are ample examples to help you learn the various commands. On that parting note, until next time, see ya !! Goodbye !! :)~~