Connect with us

Hi, what are you looking for?

Tech BableTech Bable

Software

PowerShell CD (Set-Location) Explained With Examples

PowerShell CD
PowerShell CD

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.

I’ll kick off this article with an overview of the CD (Set-Location) command. Then, in section two, I’ll explain the syntax and parameters of the command.

In section three, you’ll see the applications and examples of the knowledge you gained from sections one and two.

I also included an FAQ section where I answer questions most people ask about PowerShell CD (Set-Location).

Browse Post Topics

PowerShell CD (Set-Location): 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, the path to a folder, or a 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 or the Set-Location 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 PowerShell CD (Set-Location) Command

Syntax And Parameters Of The PowerShell CD (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 Name PowerShell CD (Set-Location) Parameter Meaning/Notes
Path Use 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.
StackName The 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.
UseTransaction This 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.
PassThru Like 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 (Set-Location) Examples

PowerShell CD (Set-Location) 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.

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

This is one of the tasks you will perform 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.

How To Use PowerShell CD (Set-Location) 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

How To Use PowerShell CD (Set-Location) 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 that 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 changed 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 PowerShell Set-Location To CD 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 the 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 the “Program 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 Program 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 backslash 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 the 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 is mapped to a local drive, you can change the directory to that local drive. 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:\

How To 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 the 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.”

How To 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

How To 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 the 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 the directory to the “Program Files(x86)” folder

How To Use PowerShell CD (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 the 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…

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

To change the 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 the 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 About PowerShell CD (Set-Location)

Frequently Asked Questions About PowerShell CD (Set-Location)
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 the directory to drive C in PowerShell, run the Set-Location command and specify C:\ as the path –

Set-Location C:\

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. For example, to return the full path to a text file, I’ll run the command –

Get-ChildItem path-to-text-file | Select-Object FullName -ExpandProperty FullName

Alternatively, to return the folder path the file is saved, I’ll call the DirectoryName property of the Get-ChildItem command –

Get-ChildItem path-to-text-file | Select-Object DirectoryName -ExpandProperty DirectoryName

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. Here is an example –

Get-ChildItem D:\ -Recurse -File

The above command will return ALL files in drive D and all its subfolders.

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. Here is a sample command –

Get-ChildItem path-to-text-file | Select-Object Name -ExpandProperty Name

My Final Thoughts About PowerShell CD (Set-Location)

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, I’ll appreciate it if you can spare just 2 minutes to share your thoughts at  Techbable Community Forum.

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

References And Further Reading

  1. Set-Location (Microsoft.PowerShell.Management) – PowerShell | Microsoft Learn
  2. PowerShell Desktop Variable – Stack Overflow
  3.  Techbable Community Forum
Click to comment

Leave a Reply

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

You May Also Like

Laptop

Google Pixelbook 12in is the latest Chromebook invention with advanced features and an elegant appearance. Chromebooks are different from older PCs and laptops. They...

Apps

NFTs and stocks are the most well-known types of speculation these days. Either way, both choices will provide funders with many benefits, but they...

Laptop

Google Pixel m3 Slate is a maddening device to review. It’s the latest in a line of premium, expensive Chrome OS devices from Google...

Gadgets

Alienware Aurora 2019 completes the legendary desktop world of high-performance gaming. This powerful technology features a next-generation Intel Core i7 9700 processor inside 8GB...

mersin escort -
Zero Waste Shop
mersin escort -
Zero Waste Shop