100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Registry and Environment

Learn how PowerShell exposes the Windows Registry and environment variables as navigable drives, and how to safely read and modify both.

AutomationIntermediate10 min readJul 10, 2026
Analogies

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.

powershell
# 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

Was this page helpful?

Topics covered

#Programming#PowerShellStudyNotes#RegistryAndEnvironment#Registry#Environment#PSDrive#Reading#StudyNotes#SkillVeris#ExamPrep