PowerShell Administration Foundations
Learn what PowerShell is, how to start it, read cmdlet syntax, discover commands, use aliases, get help, and manage files and folders.
Content
Get-Location and Set-Location
Versions:
Watch & Learn
AI-discovered learning video
Sign in to watch the learning video for this topic.
PowerShell, Where Am I? The No-Chill Tour of Get-Location and Set-Location
You wouldn't drive blindfolded. So why run scripts without knowing where you are?
We already flirted with the File System provider and learned to ask for directions with Get-Help. Now let’s get existential: “Where am I?” and “Where do I go?” In PowerShell, those questions map to two foundational cmdlets: Get-Location (current place) and Set-Location (go somewhere else).
Spoiler: it’s not just about folders. This is about moving through PowerShell’s whole provider universe—file system, registry, environment variables, certificates—like you’ve got a backstage pass and your name is on the list.
The Big Idea: Location Is a Mood (and a Provider)
- Location = your current spot in a given provider. If you’re in
C:you’re probably in folders. If you’re inHKLM:you’re inside the Windows Registry.Env:is a forest of environment variables. Different vibes, same navigation verbs. - Each provider is like a different city.
Get-Locationis your GPS pin.Set-Locationis you teleporting.
Think beyond “folders.” PowerShell providers turn different data stores into navigable places. The address bar isn’t just for
C:\; it’s forHKCU:\,Cert:\,Env:, and beyond.
Get-Location: Your Shell’s “Where Am I, Exactly?”
- Purpose: Returns your current location.
- Aliases:
pwd,gl(yes,pwdlike Bash, because PowerShell is friendly like that). - Output: A
PathInfoobject with useful properties (likePath,Drive,Provider).
Example:
Get-Location
# or
pwd
Sample output:
Path
----
C:\Users\Ada\Projects
Want to store it (because future you will inevitably wander off)?
$here = Get-Location
# later...
Set-Location $here
Pro move: pipe or inspect it.
Get-Location | Format-List *to see deep details.
Set-Location: Choose Your Own Adventure
- Purpose: Changes your current location.
- Aliases:
cd,chdir,sl. - Parameters you’ll actually use:
-Path: the destination (supports wildcards).-LiteralPath: exact path, no wildcard interpretation.-PassThru: return the resulting location object (nice for scripting).
Basic usage:
Set-Location C:\Windows
cd .. # go up one level
cd .\Logs # relative path
cd ~ # home sweet home
cd "C:\Program Files" # spaces need quotes
# Return info as an object
Set-Location C:\Windows -PassThru | Select-Object Path
If you remember from provider navigation: relative paths work, and
./..mean the same thing across providers that support them.
Absolute vs. Relative: The Eternal Struggle
- Absolute path: full address. Independent of where you are.
- Example:
C:\Users\Ada\Downloads
- Example:
- Relative path: relative to your current spot.
- Example: from
C:\Users\Ada,cd .\Downloads
- Example: from
Quick sanity checks:
Get-Location
cd .. # go up
cd . # re-affirm existence
cd ~ # home
Relative paths are elite for speed. Absolute paths are elite for certainty. Choose based on how chaotic your day is.
Drive-Hopping Like a Pro (C:, D:, HKLM:)
PowerShell remembers a separate “current location” per drive. That means:
cd D:\Projects\App
cd C:\Windows
cd D: # returns to D:\Projects\App (no backslashes = switch to last location on that drive)
cd D:\ # explicitly go to D:\ root
This also works with non-file system drives (providers):
cd HKLM:\Software\Microsoft
cd HKCU: # switch to current HKCU location
Tiny detail, big payoff:
cd D:is different fromcd D:\. One remembers, the other resets.
Providers Beyond Folders (Registry, Env, Cert)
Let’s apply those moves outside the file system.
- Registry:
cd HKLM:\Software\Microsoft\Windows\CurrentVersion
Get-ChildItem # list keys like they’re directories
- Environment variables (it’s a provider root):
cd Env:
Get-ChildItem # lists environment variables as items
- Certificates (on Windows):
cd Cert:\LocalMachine\My
Get-ChildItem # certs show up like files, drama-free
Same verbs. Different universes. Your brain likes patterns; PowerShell delivers.
Special Characters, Spaces, and “Why is my path yelling?”
- Use quotes for spaces:
cd "C:\Program Files" - Use
-LiteralPathif your path contains wildcard characters (*,?,[ ]) that you don’t want interpreted:
Set-Location -LiteralPath '.\[qa]?builds' # exact folder name, no wildcard magic
- UNC paths? Yes, you can cd to them:
cd \\fileserver\teamshare\Specs
-Pathis adventurous (wildcards allowed).-LiteralPathis that friend who refuses to play guessing games.
Quick Reference: The Core Moves
| Cmdlet/Alias | What it does | Typical Usage | Works across providers? |
|---|---|---|---|
| Get-Location, pwd, gl | Shows current location | Get-Location |
Yes, where navigation exists |
| Set-Location, cd, sl | Changes current location | cd C:\Windows, cd HKLM: |
Yes, provider-dependent |
| -LiteralPath | Treats path literally (no wildcard) | Set-Location -LiteralPath '.\[temp]' |
Yes |
| -PassThru | Returns the new location object | Set-Location C:\ -PassThru |
Yes |
If you need usage details or examples fast:
Get-Help Set-Location -ExamplesandGet-Help Get-Location -Full.
Common Pitfalls (and How to Dodge Them Stylishly)
- “I ran
Remove-Item * -Recursein the wrong place.”- Ritual: do
Get-Locationbefore destructive commands. Make it a reflex.
- Ritual: do
- “
cd D:didn’t go to the root!”- Feature, not bug. It switches to the last remembered location for
D:. Usecd D:\for root.
- Feature, not bug. It switches to the last remembered location for
- “My path has brackets and now the universe burns.”
- Use
-LiteralPathor escape characters. Or both, if it’s that kind of day.
- Use
- “Why doesn’t
..work here?”- Some providers are flat (like
Env:). You cancd Env:, but there’s no parent to climb to. Not every provider has a directory hierarchy.
- Some providers are flat (like
Scripts That Travel Well: A Few Patterns
- Capture-return pattern:
$orig = Get-Location
try {
cd C:\Windows\System32
# ... do the thing ...
}
finally {
Set-Location $orig
}
- One-liner with
-PassThrufor logging:
Set-Location HKCU:\Software -PassThru | Select-Object -ExpandProperty Path
- Combine with provider exploration from last time:
# You learned Get-ChildItem for browsing; pair it with location changes.
cd HKLM:\Software
Get-ChildItem | Where-Object Name -like '*PowerShell*'
Location discipline = script stability. Your future self sends heart emojis.
Speedrun Tips for Everyday Use
- Tab completion is your co-pilot. Start typing a path, then smash Tab.
- Use aliases for muscle memory (
pwd,cd,gl,sl). - Per-drive memory is powerful. Hop between
C:andD:without losing your place. - Treat providers as neighborhoods. Same verbs, new scenery. Check
Get-PSDriveto see who’s available.
Get-PSDrive | Select-Object Name, Provider, Root
Wrap-Up: You Are Here. Go Anywhere.
- Get-Location tells you where you are across PowerShell’s provider universe.
- Set-Location moves you deliberately—file system, registry, env, certs—same muscle memory, different data.
- Absolute vs relative paths: choose speed or certainty.
- Quotes for spaces,
-LiteralPathfor special characters,-PassThrufor script-friendly returns.
Final thought: Confidence in scripts starts with knowing your location. The difference between “legendary admin” and “who deleted prod?” is often one
Get-Locationaway.
Keep this energy as we keep layering skills. Next time we’ll stack locations like pancakes and pop back like a magician. For now: you are here; go everywhere, on purpose.
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!