As in real estate, location is everything in PowerShell. When working in a PowerShell shell or console, you have to know how to navigate. Location also matters when it comes to running commands or PowerShell scripts. The basic navigation skills haven’t changed much and even better once you learn them it doesn’t matter (much) if you are trying to navigate your C: drive or the registry.

To change locations, use the Set-Location cmdlet. Fortunately, this cmdlet uses the more familiar cd alias. This alias, from the legacy command shell, is the change directory command and you can use it pretty much the same way.

PS C:\> cd d:\temp
PS D:\temp>

In PowerShell 3.0 you can use a location of .. to indicate the parent directory.

PS D:\temp> cd..
PS D:\>

In PowerShell 2.0 there was a bug where you needed a space between the alias and .., at least I think that was the case. In any event, in PowerShell 3 either way will work.

PS D:\> cd temp
PS D:\temp> cd ..
PS D:\>

You can combine multiple instances of ..

PS C:\scripts\TechMentor2013\Automating AD with PowerShell> cd ..\..
PS C:\scripts>

Or use this to indicate a different path without having to cvhange locations:

PS C:\scripts\TechMentor2013\Automating AD with PowerShell> dir ..\..\*.ps1 | measure-object

Count : 1475
Average :
Sum :
Maximum :
Minimum :
Property :

The other shortcut that trips people up is the single period. This represents the current directory or location. To run a PowerShell command or any command that isn’t a part of your $ENV:Path environmental variable, you need to reference the current path.

PS C:\scripts> .\MyScript.ps1

The confusing part for beginners comes when you need to dot-source a script file. “Dot sourcing” tells PowerShell to keep things like functions and aliases in the current scope. If you are dot sourcing a script in the existing directory you need a period for the dot source directive followed by a space and then the path to the script, which requires another period.

PS C:\scripts>. .\MyTools.ps1
If you want to get to the root of any drive use a single \.
PS C:\scripts\TechMentor2013\Automating AD with PowerShell> cd \
PS C:\>

If you need to change to a directory that has a space, you’ll need to enclose it in quotes. Single, or double quotes will work.

PS C:\> cd "program files"
PS C:\program files> cd \
PS C:\> cd 'program files'
PS C:\program files>

Or you can escape the space with the backtick character.

PS C:\> cd program` files
PS C:\program files>

Personally, in these situations I take advantage of tab completion.

PS C:\> cd prog[TAB]

I start typing the folder name and hit TAB until I get the folder I want. PowerShell will expand the folder name for me.

PS C:\> cd '.\Program Files'

In fact, I can move the cursor to the end, add a backslash and press TAB again to continue drilling down without having to know the full folder name in advance or do much typing.

PS C:\> cd '.\Program Files'\[TAB]

You may also have realized you can change drives like this:

PS C:\scripts> e:
PS E:\>

What you probably didn’t realize is that “E:” is actually a pre-defined function as you can see in the screenshot below.

Drive letters are PowerShell functions

These functions are defined automatically for you when you start PowerShell. If you look at them you’ll discover all they do is a simple Set-Location command.

PS E:\> dir function:e: | select scriptblock

Set-Location E:

Keep in mind that changing to a different drive will return you to the last location on that drive.

PS E:\> c:
PS C:\scripts> cd .\TechMentor2013
PS C:\scripts\TechMentor2013> d:
PS D:\> c:
PS C:\scripts\TechMentor2013>

When I changed location from D: back to C:, PowerShell put me in the last directory. If you want to go to the root of another drive don’t be fooled into thinking you can do this:

PS D:\>c:\

You will get an error. You will need to use Set-Location, or its cd alias.

PS D:\> cd c:\

The last special location holder I want to show you is ~. This should take you to your user profile folder.

PS C:\> cd ~
PS C:\Users\Jeff>

You can also use it to construct a path.

PS D:\> cd ~\documents
PS C:\Users\Jeff\documents>

This is very handy because you don’t have to try and figure out the user’s home profile path.

Set-Location also has aliases of chdir and sl, if either of those is more familiar or comfortable. In another article I’ll discuss some other location tricks.

  1. Soeren P 10 years ago

    deeply sorry! wanted to give a thump up but apparently I have ten thumps and I clicked the wrong one 🙁
    Great article for me – Im still a powershell beginner

  2. Moorgan 3 years ago

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account