Change Directory in PowerShell

Change directory in PowerShell

Using Set-Location Cmdlet

Use the Set-Location cmdlet to change directory in PowerShell.

Above, we have used the Set-Location cmdlet to change the directory from C:\ to D:\ drive. In PowerShell, Set-Location is used to specify the current working directory. First, the working directory was C:\Users\DELL after using Set-Location -Path D:\ current working directory changed to D:\. Here, the -Path parameter was used to specify the path we wanted to navigate. Let’s see another example:

In this example, we used set-location to change the current directory, which was D:\ in our case, to F:\ drive.

We have used set-location instead of Set-Location and -path instead of -Path. It means PowerShell is case-insensitive in terms of commands and parameters. It can be used in either lower-case or upper-case.

One thing is also important to note -path is an optional parameter. We can also omit it. Consider the below example to see this.

In the above command, set-location was used without the -path parameter to change the current directory (F:\) to C:\ drive. Will this command work if we have a space in the directory path? Consider the following example where we want to change a directory to a path having space in it like D:\U – A. Check below to see how to do this:

In the above code, there is a space in the path; that’s why we enclosed it in double quotes. Likewise, in PowerShell, if there is a space in the directory path where you want to navigate, enclose the path in double quotes as "D:\U - A".

Set-Location has three aliases that can be used instead of Set-Location to change the current working directory. The three built-in PowerShell aliases of set-location are: sl, cd, and chdir. Each of them can be used by replacing with Set-Location.

Let’s use these aliases one by one and see how it works.

Use cd Alias

Use the cd alias to change a directory from C:\Users\DELL to D:\Downloads\Sublime in PowerShell.

Here, the cd alias was used to change the directory. Our current working directory is the Sublime folder inside the Downloads folder in the D:\ drive. Let’s consider a scenario where we want to drop one step down in the directory hierarchy. For this, use cd..; have a look at the below example to see how to use it:

In the PowerShell snippet, cd.. was used to drop one level down from the current working directory. So first, we were in the D:\Downloads\Sublime directory; after using the cd.. command, the directory dropped one level down to D:\Downloads.

Now if we want to come directly to the root directory from any folder inside the current directory using cd\ instead of cd.. as shown in the below example:

Here, cd\ was used to navigate from D:\Downloads\Sublime to the root directory D:\.

We have seen cd work the same way as the set-location cmdlet, but there is one minor difference between them. If we want to drop one level down in our path hierarchy using the set-location cmdlet, we need to add a space between the command and the two periods(..), unlike cd..; otherwise, an error will occur.

If we execute the above command, the console will display an error. So, the correct command is as follows:

Our directory was changed from D:\Downloads to D:\.

Use chdir Alias

Use the chdir alias to change a directory from C:\ to D:\Downloads\Sublime in PowerShell.

In this example, the chdir alias navigated from C:\ drive to D:\Downloads\Sublime path. Now suppose we want to navigate to a subfolder from our current working directory, but we are not sure of the exact path of the folder; for that purpose, there is a Get-ChildItem cmdlet in PowerShell that can be used to see all the folders inside the current folder. As shown in the example below:

As shown in the above example, the Get-ChildItem was used to display the list of all the folders inside the D:\Downloads\Sublime directory. So now, we can navigate to any folder where we want to move.

DIR is an alias of Get-ChildItem. We can use it to see the list of folders within the current working directory.

Use sl Alias

Use the sl alias to change a directory from D:\Users\DELL to C:\\ in PowerShell.

Using Push-Location Cmdlet

To change a director in PowerShell:

  • Use the Get-Location cmdlet to get a current working directory.
  • Use Push-Location to push the current working directory to stack and make the given directory the current working directory.
  • Use Get-Location again to see the current working directory.

How are we changing the directory in the above code? Before going into details, it is essential to know why the Push-Location cmdlet was used. When we change the directory location, we often want to trace back to the recent location. In PowerShell, the Push-Location cmdlet is used to keep track of a history of paths of directories where we moved in the form of a stack.

In the above code, we used Get-Location to know the current working directory, which was D:\U – A\testing. Then, we used the Push-Location cmdlet to push it to the stack and navigate to another folder/path, which we specified using the -Path parameter.

After the execution of the Push-Location cmdlet, the location path D:\U – A\testing was pushed into the stack, and the directory path was changed to C:\ drive. Then, to ensure the updated working directory, we used Get-Location again and observed that the current location was successfully changed to the C:\ directory; see the above output.

Let’s say we want to move to the testing folder in D: \ drive.

In the above example, first, we were in C:\ drive. After using the Push-Location -Path "D:\U- A\testing" command, the C:\ drive on which we were working moved to the stack, and the current working directory is changed to D:\U- A\testing. The -PassThru parameter displayed the new path where we moved.

The PowerShell -PassThru parameter was used with the commands with no default output. For the execution of set-location or push-location, the outcome is not displayed on the console. However, the directory changed. Check the next section to see how to return to our previous location.

Using Pop-Location Cmdlet

To change a director in PowerShell:

  • Use the Get-Location cmdlet to retrieve the current directory.
  • Use the Get-Location cmdlet with -Stack to display all the directory paths we worked on.
  • Use the Pop-Location cmdlet to pull out the path inserted in the stack.

First, we used Get-Location to know the current working directory; then, we used this cmdlet with the -Stack parameter to show all the directories we have worked on. Finally, we used the Pop-Location cmdlet to pull out the path inserted in the stack.

The above output demonstrated that our current working directory was initially D:\U- A\testing. The C:\ and D:\U – A were the location paths we previously worked on; note that these are the same paths we pushed in the stack in the above section.

Next, to go back to the previous directory on which we were working, which is stored in the stack, we used the Pop-Locationcmdlet and got the path out of the stack. We can observe that the C:\ drive was inserted last in the stack and popped out first. So we can verify the LIFO concept here. So now our current working directory is changed to C:\.

Let’s pop out the remaining path from the stack.

The remaining directory path D:\U – A popped out from the stack and became our current working directory.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *