The Registry as a PSDrive
PowerShell mounts the Windows Registry as two PSDrives by default, HKLM: for HKEY_LOCAL_MACHINE and HKCU: for HKEY_CURRENT_USER, so you can Set-Location into a registry key exactly like a folder, for example Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion. This means the same navigation cmdlets used for the file system, Get-ChildItem, Push-Location, and Set-Location, all work against the registry, though registry keys map to folders and registry values map to item properties rather than files.
Cricket analogy: HKLM: and HKCU: acting like drives is like the IPL and international calendars both using the same BCCI scheduling software, so a fixture is entered the same way whether it's a franchise or national match.
Reading and Modifying Registry Values
Get-ItemProperty retrieves the values under a registry key, such as Get-ItemProperty -Path 'HKLM:\Software\MyApp' -Name 'Version', while Set-ItemProperty changes an existing value and New-ItemProperty creates a new one with a specified -PropertyType such as String, DWord, or Binary. Remove-ItemProperty deletes a single value without deleting the whole key, whereas Remove-Item on the key path itself deletes the entire key and everything under it, so it is important to be precise about whether you intend to remove a value or a key.
Cricket analogy: New-ItemProperty adding a DWord value is like a scorer adding a new statistical column, such as strike rate, to a scorecard that previously only tracked runs and balls.
Environment Variables and the Env: Drive
PowerShell exposes environment variables through the Env: PSDrive, so $env:PATH, $env:USERPROFILE, and $env:COMPUTERNAME are all readable and, for the current session, writable simply by assigning to $env:VarName, for example $env:PATH += ';C:\Tools'. This session-scoped change disappears when the console closes; to persist a variable at the user or machine level, use [Environment]::SetEnvironmentVariable('VarName', 'Value', 'User') or 'Machine', which writes into the registry keys that actually back environment variables.
Cricket analogy: A session-only $env:PATH change is like a substitute fielder brought on for one over; once the over ends, the original fielding position returns, just as the variable resets when the console closes.
# Read and modify a registry value
$keyPath = 'HKCU:\Software\MyCompany\MyApp'
if (-not (Test-Path $keyPath)) {
New-Item -Path $keyPath -Force | Out-Null
}
New-ItemProperty -Path $keyPath -Name 'Theme' -Value 'Dark' -PropertyType String -Force
Get-ItemProperty -Path $keyPath -Name 'Theme'
# Add a folder to PATH for this session, then persist it for the current user
$env:PATH += ';C:\Tools'
[Environment]::SetEnvironmentVariable('PATH', $env:PATH, 'User')Editing HKLM: keys can affect every user on the machine and, if done carelessly, can break Windows components or installed applications. Always back up a key with reg export or Export-Clixml before running Set-ItemProperty or Remove-Item against HKLM:, and prefer HKCU: for changes that only need to apply to the current user.
- HKLM: and HKCU: are built-in PSDrives that let you navigate the registry with the same cmdlets used for the file system.
- Get-ItemProperty reads registry values; Set-ItemProperty modifies them; New-ItemProperty creates them with a specified type.
- Remove-ItemProperty deletes a single value, while Remove-Item on a key path deletes the entire key and its contents.
- The Env: drive exposes environment variables like $env:PATH for reading and session-scoped writing.
- [Environment]::SetEnvironmentVariable persists a variable at User or Machine scope by writing to the registry.
- Session-scoped $env: changes disappear when the console closes unless persisted explicitly.
- HKLM: changes affect the whole machine, so back up keys before editing and prefer HKCU: when possible.
Practice what you learned
1. Which PSDrive corresponds to HKEY_CURRENT_USER?
2. What is the difference between Remove-ItemProperty and Remove-Item when working with the registry?
3. How do you make an environment variable change persist beyond the current PowerShell session?
4. Which cmdlet would you use to create a new DWord registry value?
5. Why should you be especially cautious editing keys under HKLM:?
Was this page helpful?
You May Also Like
Working with Files and Folders
Learn how PowerShell treats the file system as a navigable provider, and how to create, read, write, copy, move, and delete files and folders with dedicated cmdlets.
Processes and Services
Learn how to inspect, start, stop, and control Windows processes and services using PowerShell's process and service management cmdlets.
Remoting with PowerShell
Learn how PowerShell Remoting uses WinRM to run commands on remote machines, including interactive sessions, one-off invocations, and persistent connections.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics