PowerShell offers a few different ways to change the directory. The official cmdlet to change the directory is Set-Location
and it has an aliases: cd
, sl
, and chdir
. PowerShell’s Set-Location
is more powerful than Command Prompt’s change directory cd
. Set-Location
can change directories, change to registry, and change to certificates, and change to environment variables.
I will refer to the cmdlet as Set-Location
but they can be swapped out with any of the aliases mentioned earlier. cd
being common one used.
Change Directory with Set-Location
In PowerShell, to change the directory we can use Set-Location
.
Set-Location -Path C:\
# or simply
Set-Location C:\
If our path has a space in it we need to use quotes or double quotes:
Set-Location 'C:\Program Files'
If we want to display the current location path we can use -PassThru
:
Set-Location 'C:\Program Files (x86)\' -PassThru
Output:
Path
----
C:\Program Files
ℹ️ Lots of other cmdlets use -PassThru
to output results from the cmdlet.
We can save the results of the -PassThru
to a variable.
$result = (Set-Location -Path 'C:\Program Files' -PassThru)
$result
will contain $result.Path
which will contain the path.
Change Drives
We can change drives and directories at the same time.
Set-Location Z:\games
In the next sections, Environment variables and Registry act like drives. We can change to them in the same way as regular device drives.
Change Directory to the Registry
If you want to set location to HKEY_LOCAL_MACHINE
use HKLM:\
:
Set-Location HKLM:\
Output contents with ls
:
Hive: HKEY_LOCAL_MACHINE
Name Property
---- --------
HARDWARE
SAM
SOFTWARE
SYSTEM
The other root registry locations are:
Root Registry Location | Set-Location |
---|---|
HKEY_CLASSES_ROOT |
HKCR:\ ? |
HKEY_CURRENT_USER |
HKCU:\ |
HKEY_LOCAL_MACHINE |
HKLM:\ |
HKEY_USERS |
HKU:\ ? |
HKEY_CURRENT_CONFIG |
HKCC:\ ? |
Set Location to Environment Variables
We can change the location to the environment variables:
Set-Location Env:\
Then if we do an ls
we can see all the environment variables:
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\Jill\AppData\Roaming
ChocolateyInstall C:\ProgramData\chocolatey
...etc...
The output will be similar to Get-ChildItem env:
to display the environment variables.
Set Location to Certificates
We can change the location to the certificates on the system:
Set-Location Cert:\
Navigate forward and backward
Navigate between forward and backward directories using +
and -
arguments. This is useful if you make a directory change and want to go back to the previous path and not up a directory (e.g., cd ..
).
Set-Location +
# or simply:
cd +
Set-Location -
# or simply:
cd -
Works in Command Prompt too!
Bonus
Where does Set-Location
with no arguments take you? To user’s home directory, which is the same as $env:USERPROFILE
.
Set-Location common places to go
Here are other default locations that are commonly used:
Variable | Path |
---|---|
~ |
<User> ’s home |
~\Desktop |
<User> ’s Desktop |
$env:ALLUSERSPROFILE |
C:\ProgramData |
$env:APPDATA |
C:\Users<User>\AppData\Roaming |
$env:CommonProgramFiles |
C:\Program Files\Common Files |
$env:CommonProgramFiles(x86) |
C:\Program Files (x86)\Common Files |
$env:CommonProgramW6432 |
C:\Program Files\Common Files |
$env:ComSpec |
C:\Windows\system32\cmd.exe |
$env:DriverData |
C:\Windows\System32\Drivers\DriverData |
$env:LOCALAPPDATA |
C:\Users<User>\AppData\Local |
$env:OneDrive |
C:\Users<User>\OneDrive |
$env:ProgramData |
C:\ProgramData |
$env:ProgramFiles |
C:\Program Files |
$env:ProgramFiles(x86) |
C:\Program Files (x86) |
$env:ProgramW6432 |
C:\Program Files |
$env:PUBLIC |
C:\Users\Public |
$env:SystemRoot |
C:\Windows |
$env:TEMP |
C:\Users<User>\AppData\Local\Temp |
$env:TMP |
C:\Users<User>\AppData\Local\Temp |
$env:USERPROFILE |
C:\Users<User> |
$env:windir |
C:\Windows |
ℹ️ <User>
being equivalent to $env:USERNAME
Happy Coding! 👩💻