The Chalkboard


Fix broken PATH on Windows systems

Last Updated: [2023-04-06 Thu 06:43]

Occasionally the following error will appear when attempting to run commands on Windows machines:

The term 'cmd' is not recognized as the name of a cmdlet

This is due to a Path variable being set incorrectly in the registry. This can happen with some poorly written automation, or a misbehaving application, however, it's fairly easy to fix, and I have provided a Powershell script to address this (Run as Administrator):

$registryPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
$registryValue = "Path"

If ((Get-Item -Path Registry::$registryPath).GetValueKind($registryValue) -ne "ExpandString") {
  $pathData = (Get-Item -Path Registry::$registryPath).GetValue($registryValue)
  Set-ItemProperty -Path Registry::$registryPath -Name $registryValue -Value $pathData -Type ExpandString
  Write-Output "Registry key updated"
} else {
  throw "Key has the correct type. Aborting"
}

You can also do this manually in Registry Editor:

  1. Launch regedit as Administrator.
  2. Go to the following:

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

From there:

  1. If the "Path" entry is a String (REG_SZ) then you have just confirmed the issue.
  2. Copy the contents of Path, and paste it into Notepad for safe keeping.
  3. Delete the "Path" entry, and create a new Expandable String Value (REG_EXPAND_SZ)
  4. Paste the contents from the previous "Path" string.
  5. Close regedit.
  6. Restart the machine.

DISCLAIMER: The information provided on this website is generated from my own notes and is provided "as is" and without warranties. Robert Ian Hawdon can not be held responsible for damages caused by following a guide published on this site. This website contains links to other third-party websites. Such links are provided as convienice of the reader. I do not endorce the contents of these third party sites.