How to Change Directory in PowerShell?

Do you need to know how to change the directory in PowerShell? In this tutorial, I will show you different methods to change the directory in PowerShell.

To change directories in PowerShell, you can use the cd command followed by the path of the directory you wish to navigate to. For example, to switch to the C:\Windows\System32 directory, you would type cd C:\Windows\System32 and press Enter.

Basic Commands to Change Directory

Now, let me show you some basic commands to change the directory using PowerShell.

Using cd Command

The cd command, short for “change directory,” is one of the most commonly used commands in PowerShell for this purpose. Its usage is like the below:

cd C:\Path\To\Your\Directory

For example, to navigate to the C:\Windows\System32 directory, you would use:

cd C:\Windows\System32

Using Set-Location Cmdlet

PowerShell also provides the Set-Location cmdlet, which is functionally equivalent to cd but offers more flexibility and readability, especially in scripting:

Set-Location C:\Path\To\Your\Directory

For instance, to change to the C:\Users directory, you can use:

Set-Location C:\Users

Using sl Alias

For those who prefer shorter commands, sl is an alias for Set-Location:

sl C:\Path\To\Your\Directory

This alias can be particularly useful for quick navigation. For example:

sl C:\Temp

Check out How to Use Wildcards in PowerShell Switch?

Navigate Relative Paths using PowerShell

Now, let me show you how to navigate to relative paths using PowerShell.

Moving Up the Directory Tree

To move up one level in the directory tree, use:

cd ..

For example, if you are in C:\Users\YourName\Documents and you want to go to C:\Users\YourName, simply execute:

cd ..

Moving to the Root Directory

To navigate directly to the root of the current drive, use:

cd \

For instance, if you are deep within a directory structure like C:\Users\YourName\Documents\Projects, typing cd \ will take you back to C:\.

Check out Get Current Directory in PowerShell

Advanced Directory Navigation using PowerShell

Now, let me show you some advanced directory navigation using PowerShell.

Using Push-Location and Pop-Location

PowerShell offers Push-Location and Pop-Location cmdlets to manage a stack of locations. This is particularly useful when you need to temporarily switch to another directory and then return to the original one.

To push the current location onto the stack and change to a new directory:

Push-Location C:\New\Directory

To return to the previous directory:

Pop-Location

Change to Previous Directory

If you want to switch back to the previous directory you were in, you can use:

cd -

This is similar to the cd - command in Unix-based systems. For example, if you switch from C:\Users to C:\Windows and then want to go back to C:\Users, simply execute:

cd -

Permissions

Ensure you have the necessary permissions to access the directory you are trying to navigate to. Running PowerShell as an administrator can sometimes resolve permission issues.

Conclusion

In this tutorial, I have explained how to change a directory using PowerShell. You can use various commands, such as the cd Command, Set-Location Cmdlet, and sl Alias.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.