PowerShell CD (Set-Location) Explained with Examples

Photo of author

By Victor Ashiedu

Published

Are you new to PowerShell and want to learn PowerShell CD (Set-Location) command? This guide teaches you all you need to know about changing the directory in PowerShell with CD or Set-Location.

CD (Change Directory) is the alias of the Set-Location PowerShell cmdlet.

Overview

PowerShell CD (Set-Location): Overview

Like the CD (Change Directory) command, Set-Location changes the current working location to a location you specify. Talking about “working location”, this could be a drive letter, path to a folder or registry hive.

In addition to setting the current location to a folder, you could also set it to a subfolder. After setting the current location, you can run commands to target the current location without having to specify the path exclusively in your commands.

For example, if I want to run a PowerShell script in “D:\PowerShell Scripts\Get-Windows11Compatibility”, I will run the command below to set my current location to the folder.

Set-Location "D:\PowerShell Scripts\Get-Windows11Compatibility"

Once I set the current location with the CD command, I can run a PowerShell script in that location by adding a period and backslash (.\), followed by the tab key (to select available scripts in the location.

Syntax and Parameters of the Set-Location Command

The Set-Location cmdlet has three syntaxes:

Set-Location
   [[-Path] <String>]
   [-PassThru]
   [-UseTransaction]
   [<CommonParameters>]
Set-Location
   -LiteralPath <String>
   [-PassThru]
   [-UseTransaction]
   [<CommonParameters>]
Set-Location
   [-PassThru]
   [-StackName <String>]
   [-UseTransaction]
   [<CommonParameters>]

In the table below, I have explained each parameter in the Set-Location syntaxes.

Parameter NamePowerShell CD (Set-Location) Parameter Meaning/Notes
PathUse the Path parameter to specify the path of a new working location. The Path parameter accepts wildcards.
LiteralPath Use the LiteralPath parameter to specify a path of the location. The value of the LiteralPath parameter is used exactly as it is typed. The difference between the Path and the LiteralPath parameter is that Path accepts wildcards while LiteralPath requires you to specify the exact path.
StackNameThe StackName parameter specifies an existing location stack name that this cmdlet makes the current location stack. Using CD or Set-Location with the -StackName parameter does not change the current location. It only changes the stack used by the *-Location cmdlets. To list all the *-Location cmdlets, run the command: Get-Command *-Location. More in the examples section.
UseTransactionThis is a SwitchParameter – meaning that it does not require you to specify any input. When you use CD or Set-Location with -UseTransaction, PowerShell includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
PassThruLike the UseTransaction parameter, PassThru is also a SwitchParameter. Using this parameter returns a PathInfo object that represents the location. By default, the Set-Location cmdlet does not generate any output. But if you run Set-Location with -PassThru, it displays the PathInfo object of the current working directory.

PowerShell CD Examples

Welcome to the fun part of this article! In the following subsections, I’ll show you different ways you can use the CD or Set-Location command in PowerShell.

Example 1: Use Set-Location to Change to a Directory (Folder)

You will perform this task with the CD command – changing the current PowerShell location to a directory.

To change to a Directory with CD or Set-Location, use the command syntax below:

CD <folder or directory path>

For example, to change a directory to a folder called “Junctions” in drive D:\, I’ll run the command below:

CD D:\Junctions

The PowerShell prompt will change to the specified directory immediately.

Example 2: Use PowerShell CD Command to Change to a Folder with Spaces

Changing to a folder with spaces is the same as changing to a folder without spaces with one exception. Unlike changing to a folder without spaces, to CD to a folder with spaces, enclose the folder in double quotes (“”).

For example, to change to a folder called “VMWare ESXi” located in drive D:\, I’ll run the command below –

Set-Location "D:\VMWare ESXi"

To show you what would have when you CD to a folder with spaces without enclosing the folder path in quotes, I ran the Set-Location without the quotes. This threw an error message. ]

But when I ran the command and enclosed the folder path in double quotes, the command ran successfully.

How To Use PowerShell CD (Set-Location) To Change To A Folder With Spaces

Example 3: Use Set-Location Cmdlet to Change to Root Directory

The root directory in Windows is the drive letter of a partition. For example, the root directory of “D:\VMWare ESXi” is D:\. To change from the current directory to the root directory, run the CD command (or Set-Location), followed by a space, then add two periods (..).

Here is an example. To change my current directory from “D:\VMWare ESXi” to the root directory, D:\ I’ll run the command below.

CD ..

Apart from changing to the root directory of the current folder, you can also change to the system root directory. The system root directory is the folder in Windows is installed.

To change to the system root directory in PowerShell, run the CD (or Set-Location) command and specify the system root environment variable.

set-location $env:SystemDrive

When I run the above command on my PC, my PowerShell console changes its current directory to C:\Windows – my system root folder.

How To Use PowerShell CD (Set-Location) To Change To Root Directory

How To Use Set-Location Command To Change To Environment Variables

PowerShell has a long list of environment variables. The environment variables start with “$env:”

So, if you’re not sure of the name of environment variable you want to CD into, type $env: – then, press the tab key to display available environment variables.

For example, if I want to CD to “Programe Files” environment variable, I will enter $env: in my PowerShell console. Then, I’ll keep pressing the tab key until my PowerShell console displays “$env:CommonProgramFiles.”

Then, to change my current directory to Programe Files, I’ll press the enter key.

This knowledge has real-life scripting applications. So, next time you’re scripting and you need to CD to an environment variable, you know what to do!

How To Use PowerShell Set-Location To CD To A Network Drive

In Windows, you can access a network drive by entering two backslashes, followed by the name of the network server. Then, you’ll enter another back slash followed by the name of the shared network folder.

So, if you wish to CD to a network drive, you use the same method to define the path to the network share. For example to change directory to a network folder called “Tempfolder4” in a server called “ITGLTUK01”, I’ll run the command below.

CD \\ITGLTUK01\Tempfolder4
How To Use PowerShell Set-Location To CD To A Network Drive

Alternatively, if the network share in mapped to a local drive, you can change directory to that local drive. For the network path I used in the last example is mapped to my local drive Z – see the screenshot below.

I can use the PowerShell Set-Location command to CD to drive Z:\.

Set-Location Z:\

Example 4: Use PowerShell Set-Location to CD to Current Script Location

You can only CD to the current script location when you’re running a script. So, you cannot cd to current script location when you run a command on the PowerShell console.

The current script location is defined by a variable called $Script:MyInvocation.MyCommand.Path.

To get the current location of the script you’re running, use the Split-Path command as shown below:

Split-Path -Parent $Script:MyInvocation.MyCommand.Path

However, as I mentioned earlier, if you run this command in a PowerShell, it will return an error. You can only use this command to return the location of the script if the script is running.

So, to demonstrate how this works, I have written a short script that writes the current location of the script. Here is the script…

$ScriptLocation = Split-Path -Parent $Script:MyInvocation.MyCommand.Path
Write-Host "This script is located in $ScriptLocation"

In the first line of the code, I saved “Split-Path -Parent $Script:MyInvocation.MyCommand.Path” in a variable. Then, in line two, I used Write-Host to display the information “This script is located in $ScriptLocation”.

The Write-Host command will replace $ScriptLocation with the information saved in the variable – in this instance, the location of the script I am running.

To run this script, I copied it to a new PowerShell ISE document. Then, I saved the script as scriptpath.ps1.

To run the script, I opened a PowerShell console, navigated to the folder I saved the script, then called the script with…

.\scriptpath.ps1

As shown in the screenshot below, the script displayed “This script is located in D:\PowerShell Scripts,” – confirming that my script is located in “D:\PowerShell Scripts.”

Example 5: Use PowerShell Set-Location to CD to the Current User’s Desktop or Documents Folder

To get the current user’s desktop run the command below:

[Environment]::GetFolderPath("Desktop")

Then, to CD to the path, save the above command in a variable. Then, use PowerShell CD to change to the user’s desktop.

Here are the commands…

$Desktop = [Environment]::GetFolderPath("Desktop")
CD $Desktop

When I ran the two commands, my PowerShell console changed to my desktop…

How To Use PowerShell Set-Location To CD To The Current User's Desktop

Similarly, to change to the current user’s Documents folder, use the commands below.

$Documents = [Environment]::GetFolderPath("MyDocuments")
Set-Location $Documents

Example 6: Use PowerShell CD (Set-Location) Command to Change to Program Files

To change to program files, first save the Program Files environment variable to a variable. Then, run the CD or Set-Location command and specify the variable as your path.

Here are the two commands…

$PF = $env:ProgramFiles
CD $PF

Alternatively, if you want to change directory to the “Program Files(x86)” folder, use the commands below.

$PF86 = ${env:ProgramFiles(x86)}
CD $PF86
You can also run the last two commands without saving the environment variables to a variable. For example, you can use the command – cd ${env:ProgramFiles(x86)} – to change directory to the “Program Files(x86)” folder

Example 7: Use Set-Location Command to Change Directory One Step Back from Current Directory

In the last example, I changed my current location to my Documents folder path – “C:\Users\VictorA\Documents”. In this example, I want to change directory one step backward to “C:\Users\VictorA”.

I can accomplish this task with the Push-Location command shown below…

Push-Location ..

Here is the result…

On the opposite side of the coin, if my script requires me to cd forward to my previous path, I can use the Pop-Location command…

Example 8: Use PowerShell CD Command to Change Directory to a File’s Location

To change directory, to a file’s location, you can use the Get-ChildItem command to find the file’s location. Then, use Set-Location to the file’s path.

In this example, I want to find a file drivers.txt in drive D:\. Then, I’ll change my Powershell location to the path the folder is saved in.

Firstly, I’ll return the path the text file is saved with the command below.

(Get-ChildItem D:\ -Recurse | Where-Object {$_.Name -eq "drivers.txt"}).DirectoryName

The command shows that the drivers.txt is located in D:\report.

How To Use PowerShell CD (Set-Location) Command To Change Directory To A File's Location

But to change directory to this folder using the previous command without errors, I need to save the command in a variable.

$Path = (Get-ChildItem D:\ -Recurse | Where-Object {$_.Name -eq "drivers.txt"}).DirectoryName

Then, I’ll use the PowerShell CD command to change my location to the file’s saved directory…

$Path = (Get-ChildItem D:\ -Recurse | Where-Object {$_.Name -eq "drivers.txt"}).DirectoryName
CD $Path

There you go! My current PowerShell location has changed to the location of the file.

How To Use PowerShell CD (Set-Location) Command To Change Directory To A File's Location

Frequently Asked Questions

1. What is CD in PowerShell?

The CD command is an alias of Set-Location. You can use CD or Set-Location to change your current PowerShell location to a specified location.

2. How do I go to C Directory in PowerShell?

To change directory to drive C in PowerShell, run the Set-Location command and specify C as the path.

3. How do I get a file’s path in PowerShell?

To get a file’s path in PowerShell, use the Get-ChildItem command, then call the FullName property.

4. How do I get a list of files in a directory and subfolders using PowerShell?

To return a list of files in a directory and its subfolders, use the Get-ChildItem command with the -Recurse and -File parameters.

5. How do I get the filename in PowerShell?

To get a file’s name in PowerShell, run the Get-ChildItem command and return the Name property of the Get-ChildItem command. 

Conclusion

The PowerShell CD (or Set-Location) is a simple but PowerShell PowerShell command. You can use this command to change the current location in the PowerShell console.

Most importantly, you can use this command to change locations when running PowerShell scripts. This will make your scripts smarter and more intuitive.

I hope you found this guide helpful and easy to understand! If you did, click on “Yes” beside the “Was this page helpful” question below.

You may also express your thoughts and opinions by using the “Leave a Comment” form at the bottom of this page.

Finally, if you love learning PowerShell, you’ll love our PowerShell Explained articles.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment