Search code examples
windowspowershellregistry

Powershell - Remove-ItemProperty : Cannot find path because it does not exist


I have a script that removes some entries from the registry, the issue I'm having is a path I give the Remove-ItemProperty cmdlet is not being recognized.

Here is where I get the pathSID and where I define pathHKUClass:

#Set Variables for SID String
$objUser = New-Object System.Security.Principal.NTAccount($env:USERNAME)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$pathSID = $strSID.Value
$pathHKUClass = "$($pathSID)_Classes"

This is where I use the Remove-ItemProperty cmdlet with the pathHKUClass:

Remove-ItemProperty -Path "HKU:\$($pathHKUClass)\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache" -Name "$strSW*" -WhatIf

The error message I get says:

Remove-ItemProperty : Cannot find path 'HKU:\S-1-5-21-326852099-1603424837-312552118-1388315_Classes\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache' because it does not exist.

Also note: the $strSW is pulling in a file path that I use in other parts of the script:

#Locating Spiceworks.exe
$Spiceworks = Get-ChildItem "C:\Users" -Recurse -Filter "Spiceworks.exe" | Select-Object -Property FullName
$strSW = @()
$strSW += $Spiceworks.FullName

If anyone has any ideas why my $pathHKUClass variable doesn't work in this path, but running that variable in the prompt outputs the directory under HKU, please let me know.


Solution

  • The issue was an error on my end. I reviewed the path in the registry...

    This is the path I was trying to use:

    "HKU:\$($pathHKUClass)\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache"
    

    This is the real path that I should be using:

    "HKU:\$($pathHKUClass)\Local Settings\Software\Microsoft\Windows\Shell\MuiCache"
    

    Lesson learned in proofreading my own code.