1

I need to know if there is any way to delete all the instances of a given environment variable, this means from all users and from the system itself.

The problem is about a software update. I need to keep installed a software in 2 versions, the old and the newer one. During the go live weekend i need to make sure that the OLD Version environment variable is deleted from the system (User and system) and just create the new one.

The problem is that i can log in the system with the admin user but not with the real user, so if he has the environment variable in his profile is when i have the problem.

Is there any way/soft for this purpose?

Thanks a lot.

2

System variables are stored in registry in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and affect all users, so you have to delete unwanted variable only once.

User variables are in HKEY_CURRENT_USER\Environment, but HKCU is actually just a mounted key from HKEY_USERS. So if you have administrative privileges, then you can access other users' enviromental variables by editing HKEY_USERS\S-[something]\Environment.

  • Hello,Thanks for answering. The problem is this "something" i can't make a script for it, as this "something" may be different from one computer to another, and in addition if there are 2 users, it may appear two times in the registry. Rigt??????? This is why maybe "deleting" all the keys with the env-var name may be a solution. – user257234 Sep 25 '13 at 8:25
  • change it once, export the keys and import them on the other pc's. – Lorenzo Von Matterhorn Sep 25 '13 at 8:38
  • Thanks, finally im doing it by navigating and cleaning the HKEY_USERS\S-[something]\Environment manually – user257234 Sep 26 '13 at 10:11
0

Created this ps cmdlet if you wanna remove a specific environment variable for a specific user

function Remove-EnvironmentVariableForUser
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string] $User,
        [Parameter(Mandatory)]
        [string] $Variable
    )

    Set-StrictMode -Version 'Latest'

    $AdObj = New-Object System.Security.Principal.NTAccount($User)
    $strSID = $AdObj.Translate([System.Security.Principal.SecurityIdentifier])
    $Sid = $strSID.Value
    Assert-NotNull $Sid

    $Path = "Registry::HKEY_USERS\$Sid\Environment"

    $Property = Get-ItemProperty $Path -Name $Variable -ErrorAction Ignore
    if($Property) {
        Write-Warning "Removing $Variable property on path $path from user $User"
        Remove-ItemProperty $Path -Name $Variable
    }else{
        Write-Host "No such Variable exists"
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.