Powershell

How to change directory in PowerShell

PowerShell is the built-in command-line application for Windows to perform various operations using commands. The graphical user interface of windows is quite interactive enough to accomplish various computing tasks. However, PowerShell can do any task with less effort and time as compared to graphical interface support.

PowerShell allows you to create directories, create files, manage the files/directories, and many directory management operations. This article provides an informative guide to changing directories in PowerShell.

How to change directory in PowerShell

PowerShell has more than 1300 cmdlets and aliases that assist in accomplishing various tasks. Alias is an alternative to the cmdlet and provides the same output as that of the parent cmdlet.

How to use the Set-Location cmdlet in PowerShell

The “SetLocation” cmdlet processes the absolute as well as the relative path. The syntax of the SetLocation cmdlet is provided below:

Set-Location -Path ... -PassThru

The –Path parameter accepts the relative or absolute path of the directory. The –PassThru parameter allows you to execute the cmdlet even if the output is empty.

The following SetLocation cmdlet allows you to change the directory using the absolute path of the directory:

Set-Location -Path E:\TSEP -Passthru

The output shows that the current working directory of the PowerShell has been changed.

Let’s check the functionality of the Set-Location by using the relative path:

Set-Location -Path “March 2022-PassThru

The current directory of PowerShell has been changed to “March 2022“.

Note: The absolute path refers to the complete path of the directory whereas the relative path is the path of the directory according to the current working directory.

How to use cd alias in PowerShell

The cd command is an alias of the Set-Location cmdlet and serves the same purpose as the parent cmdlet does. The syntax of the cd command is provided below:

cd -Path ... -PassThru

Like the SetLocation cmdlet, the cd command also accepts the relative or the absolute path.

The following command allows the PowerShell to change its directory using the relative path:

cd -Path C:\Windows -PassThru

The output of the command shows that the current directory of the PowerShell has been changed.

How to use chdir alias in PowerShell

The chdir is an alias of the SetLocation cmdlet which can also be used to change the current directory of the PowerShell. The syntax of the chdir is given below:

chdir -Path /path/to/file -PassThru

The following example makes use of the chdir alias to change the current directory of the PowerShell:

chdir -Path addins -PassThru

How to use the sl alias in PowerShell

The “sl” is another alias of the SetLocation cmdlet in PowerShell. The working of the “sl” command depends on the following syntax:

sl -Path -PassThru

The command provided below makes use of the “sl” alias to change the current directory:

sl -Path F:\ADNAN -PassThru

It is observed that the current directory has been changed to the “F:\ADNAN”.

Conclusion

In PowerShell, the current directory can be changed using the Setlocation command and its aliases cd, chdir, and sl. This post demonstrates the possible methods to change the current directory in PowerShell. We have illustrated various examples to show you how Set-Location and its aliases can be used to change the current directory in the PowerShell.

About the author

Adnan Shabbir