diff --git a/src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs b/src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs index 468c7bb3b7..b0a7c9824f 100644 --- a/src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs +++ b/src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs @@ -118,7 +118,10 @@ public static string[] GetVariableNames(EnvironmentVariableTarget scope) { using (var registryKey = GetEnvironmentKey(scope)) { - return registryKey.GetValueNames(); + // .NET seems to cache the value names even if they're set to null (removed) at some point + // so we need to only return the names of things that have non-null values. + var names = registryKey.GetValueNames(); + return names.Where(n => !string.IsNullOrEmpty((string)registryKey.GetValue(n))).ToArray(); } } catch @@ -219,7 +222,10 @@ public static void UpdateSession(PSCmdlet cmdlet) foreach (var name in GetVariableNames(scope)) { var value = GetVariable(cmdlet, name, scope); - SetVariable(cmdlet, name, EnvironmentVariableTarget.Process, value); + if (!string.IsNullOrEmpty(value)) + { + SetVariable(cmdlet, name, EnvironmentVariableTarget.Process, value); + } } }