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.

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

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.

You can combine multiple instances of ..

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

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.

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.

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.

Or you can escape the space with the backtick character.

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

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

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.

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

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.

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

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:

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

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

You can also use it to construct a path.

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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

  1. Soeren P 6 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 1 week ago

Leave a reply

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


© 4sysops 2006 - 2020


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


Log in with your credentials


Forgot your details?

Create Account