{"record":{"id":"e41feced5cc21885","repo":"chocolatey/choco","slug":"the-environment-variable-scope-value-scope-is","errorCode":null,"errorMessage":"The environment variable scope value '{scope}' is not supported.","messagePattern":"The environment variable scope value '(.+?)' is not supported\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs","lineNumber":100,"sourceCode":"            return value ?? string.Empty;\r\n        }\r\n\r\n        /// <summary>\r\n        /// Gets the registry key associated with the targeted scope of Environment variables.\r\n        /// </summary>\r\n        /// <param name=\"scope\">The scope of the environment variables to look up.</param>\r\n        /// <returns>The registry key associated with the targeted <paramref name=\"scope\"/> of environment variables.</returns>\r\n        /// <exception cref=\"NotSupportedException\">Thrown if <paramref name=\"scope\"/> is not <see cref=\"EnvironmentVariableTarget.User\"/> or <see cref=\"EnvironmentVariableTarget.Machine\"/>.</exception>\r\n        private static RegistryKey GetEnvironmentKey(EnvironmentVariableTarget scope, bool writable = false)\r\n        {\r\n            switch (scope)\r\n            {\r\n                case EnvironmentVariableTarget.User:\r\n                    return Registry.CurrentUser.OpenSubKey(UserEnvironmentRegistryKeyName, writable);\r\n                case EnvironmentVariableTarget.Machine:\r\n                    return Registry.LocalMachine.OpenSubKey(MachineEnvironmentRegistryKeyName, writable);\r\n                default:\r\n                    throw new NotSupportedException($\"The environment variable scope value '{scope}' is not supported.\");\r\n            }\r\n        }\r\n\r\n\r\n        /// <summary>\r\n        /// Gets the list of environment variables in the specified <paramref name=\"scope\"/>.\r\n        /// </summary>\r\n        /// <param name=\"scope\">The scope to lookup environment variable names in.</param>\r\n        /// <returns></returns>\r\n        public static string[] GetVariableNames(EnvironmentVariableTarget scope)\r\n        {\r\n            if (scope == EnvironmentVariableTarget.Process)\r\n            {\r\n                return Environment.GetEnvironmentVariables().Keys.Cast<string>().ToArray();\r\n            }\r\n\r\n            try\r\n            {\r","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/chocolatey/choco/blob/0d5abdd10cc177a141e69547cad6935b419b6c17/src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs#L82-L118","documentation":"Thrown by the private EnvironmentHelper.GetEnvironmentKey helper when the supplied EnvironmentVariableTarget is neither User nor Machine. The method maps a scope to a registry hive (User -> HKCU\\Environment, Machine -> HKLM\\...\\Session Manager\\Environment); Process and any other value have no registry backing, so they hit the default branch. Public callers GetVariable/GetVariableNames short-circuit Process before reaching this method, so under the current enum (Process, User, Machine) the guard is effectively a defensive contract check for an unexpected/unsupported value.","triggerScenarios":"Calling GetVariable(cmdlet, name, scope) or GetVariableNames(scope) with an EnvironmentVariableTarget that is not User or Machine, when the caller has NOT already handled Process; or invoking GetEnvironmentKey directly (e.g. via reflection) with a non-User/non-Machine value such as a casted out-of-range integer.","commonSituations":"Custom PowerShell cmdlet/automation that casts an arbitrary int to EnvironmentVariableTarget and passes it through; a future .NET release introducing a new EnvironmentVariableTarget member not covered by the switch; unit tests that exercise the helper with unexpected enum values.","solutions":["Pass only EnvironmentVariableTarget.User or EnvironmentVariableTarget.Machine to the registry-backed code path.","Handle EnvironmentVariableTarget.Process separately by calling Environment.GetEnvironmentVariable / Environment.GetEnvironmentVariables before delegating to the registry helper.","If the enum gains a new member, add a matching case to GetEnvironmentKey's switch so it is no longer unsupported."],"exampleFix":"// before\nvar value = EnvironmentHelper.GetVariable(cmdlet, name, (EnvironmentVariableTarget)42);\n\n// after\nif (scope == EnvironmentVariableTarget.Process)\n{\n    return Environment.GetEnvironmentVariable(name, scope);\n}\nvar value = EnvironmentHelper.GetVariable(cmdlet, name, scope);","handlingStrategy":"validation","validationCode":"// Only call registry-backed helpers for User/Machine; handle Process separately.\nif (scope == EnvironmentVariableTarget.Process)\n{\n    var v = Environment.GetEnvironmentVariable(name, scope);\n    return v ?? string.Empty;\n}\nif (scope != EnvironmentVariableTarget.User && scope != EnvironmentVariableTarget.Machine)\n{\n    throw new ArgumentOutOfRangeException(nameof(scope), scope, \"Only User, Machine, Process are supported.\");\n}\nreturn EnvironmentHelper.GetVariable(cmdlet, name, scope);","typeGuard":"static bool IsRegistryBackedScope(EnvironmentVariableTarget scope)\n    => scope == EnvironmentVariableTarget.User || scope == EnvironmentVariableTarget.Machine;","tryCatchPattern":null,"preventionTips":["Treat EnvironmentVariableTarget.Process as a special case using the Environment class, never via GetEnvironmentKey.","Validate the enum is User or Machine before delegating to registry helpers.","When the BCL enum is extended, update GetEnvironmentKey's switch to cover the new member."],"tags":["environment-variables","registry","win32","powershell","notsupported"],"backgroundTag":null,"analyzedSha":"0d5abdd10cc177a141e69547cad6935b419b6c17","analyzedAt":"2026-08-13T18:33:03.301Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}