Remove part of a user path

Removing user path.

Wuth active-setup we set a HKCU:\environment\path
with a value of $envappdata\username\hcm

On uninstall, I need removing that part of the string for each user.

I was looking for a way using Invoke-HKCURegistrySettingsForAllUsers. But I cannot figure how analysing the value finding the correct string with the username and removing it. I found it is possible to add, remove or modifyig a key but not to insert some logic leading to get the correct path with the correct username. Any idea?

I’m not real clear on what it is you’re trying to do but from what I think you’re asking, it seems the -split operator would be of use here. Put your path in a variable, then use $Var -split "\\" to split the full path into its constituent parts. You could then reference the parts with $Var[0], $Var[1], etc.

$Var = "C:\one\two\three\four"
$Var2 = $Var -split "\\"
Write-Host "The variable `"Var2`" is now an array consisting of:"
$Var2[0]
$Var2[1]
$Var2[2]
$Var2[3]
$Var2[4]

Hope that helps.

Hi Francois,

You can try below script to delete/remove registry keys from all users profiles under HKCU.

$HKCURegistrySettings = {
Remove-RegistryKey -Key ‘HKEY_CURRENT_USER\environment\path’ -SID $UserProfile.SID -Recurse -ErrorAction SilentlyContinue
}
Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings