Table of Contents
Using Set-Location
Cmdlet
Use the Set-Location
cmdlet to change directory in PowerShell.
1 2 3 |
Set-Location -Path D:\ |
1 2 3 |
PS D:\> |
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:
1 2 3 |
set-location -path F:\ |
1 2 3 |
PS F:\> |
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 ofSet-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.
1 2 3 |
set-location C:\ |
1 2 3 |
PS C:\> |
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:
1 2 3 |
set-location "D:\U - A" |
1 2 3 |
PS D:\U - A> |
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 ofset-location
are:sl
,cd
, andchdir
. Each of them can be used by replacing withSet-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.
1 2 3 |
cd -Path D:\Downloads\Sublime |
1 2 3 |
PS D:\Downloads\Sublime> |
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:
1 2 3 |
cd .. |
1 2 3 |
PS D:\Downloads> |
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:
1 2 3 |
cd\ |
1 2 3 |
PS D:\> |
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.
1 2 3 |
Set-Location.. |
If we execute the above command, the console will display an error. So, the correct command is as follows:
1 2 3 |
Set-Location .. |
1 2 3 |
PS D:\> |
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.
1 2 3 |
chdir -Path D:\Downloads\Sublime |
1 2 3 |
PS D:\Downloads\Sublime> |
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:
1 2 3 |
Get-ChildItem |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Directory: D:\Downloads\Sublime Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 7/7/2020 1:13 PM Packages -a---- 9/1/2020 2:32 PM 59981 331ffde2-9273-4446-82f0-04aee4abce7e.dmp -a---- 9/1/2020 2:28 PM 58777 59dd56f7-2f0b-42d2-b1d4-80c59dc286f3.dmp -a---- 10/1/2019 9:20 PM 56720 changelog.txt -a---- 10/1/2019 10:35 PM 232336 crash_reporter.exe -a---- 10/12/2018 6:11 AM 773968 msvcr100.dll -a---- 10/1/2019 10:35 PM 6644112 plugin_host.exe -a---- 10/12/2018 6:11 AM 2628080 python3.3.zip -a---- 10/12/2018 6:11 AM 5420032 python33.dll -a---- 10/1/2019 10:35 PM 451472 subl.exe -a---- 9/17/2019 7:44 PM 38852 sublime.py -a---- 9/17/2019 7:44 PM 36887 sublime_plugin.py -a---- 10/1/2019 10:35 PM 6896528 sublime_text.exe -a---- 7/7/2020 1:13 PM 10921 unins000.dat -a---- 7/7/2020 1:13 PM 1189776 unins000.exe -a---- 7/7/2020 1:13 PM 22709 unins000.msg -a---- 10/1/2019 10:35 PM 160656 update_installer.exe |
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 ofGet-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.
1 2 3 |
sl -Path C:\ |
1 2 3 |
PS C:\> |
Further reading:
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.
1 2 3 4 5 |
Get-Location Push-Location -Path C:\ Get-Location |
1 2 3 4 5 |
Path ---- C:\ |
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.
1 2 3 |
Push-Location -Path "D:\U - A\testing" -PassThru |
1 2 3 4 5 |
Path ---- D:\U - A\testing |
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.
1 2 3 4 5 |
Get-Location Get-Location -Stack Pop-Location -PassThru |
1 2 3 4 5 6 7 |
Path ---- D:\U - A\testing C:\ D:\U - A |
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-Location
cmdlet 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.
1 2 3 |
Pop-Location -PassThru |
1 2 3 4 5 |
Path ---- D:\U - A |
The remaining directory path D:\U – A popped out from the stack and became our current working directory.