chocolatey/choco · error · NotSupportedException
The environment variable scope value '{scope}' is not suppor
Error message
The environment variable scope value '{scope}' is not supported. What it means
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.
Source
Thrown at src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs:100
return value ?? string.Empty;
}
/// <summary>
/// Gets the registry key associated with the targeted scope of Environment variables.
/// </summary>
/// <param name="scope">The scope of the environment variables to look up.</param>
/// <returns>The registry key associated with the targeted <paramref name="scope"/> of environment variables.</returns>
/// <exception cref="NotSupportedException">Thrown if <paramref name="scope"/> is not <see cref="EnvironmentVariableTarget.User"/> or <see cref="EnvironmentVariableTarget.Machine"/>.</exception>
private static RegistryKey GetEnvironmentKey(EnvironmentVariableTarget scope, bool writable = false)
{
switch (scope)
{
case EnvironmentVariableTarget.User:
return Registry.CurrentUser.OpenSubKey(UserEnvironmentRegistryKeyName, writable);
case EnvironmentVariableTarget.Machine:
return Registry.LocalMachine.OpenSubKey(MachineEnvironmentRegistryKeyName, writable);
default:
throw new NotSupportedException($"The environment variable scope value '{scope}' is not supported.");
}
}
/// <summary>
/// Gets the list of environment variables in the specified <paramref name="scope"/>.
/// </summary>
/// <param name="scope">The scope to lookup environment variable names in.</param>
/// <returns></returns>
public static string[] GetVariableNames(EnvironmentVariableTarget scope)
{
if (scope == EnvironmentVariableTarget.Process)
{
return Environment.GetEnvironmentVariables().Keys.Cast<string>().ToArray();
}
try
{
View on GitHub (pinned to 0d5abdd10c)
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.
Example fix
// before
var value = EnvironmentHelper.GetVariable(cmdlet, name, (EnvironmentVariableTarget)42);
// after
if (scope == EnvironmentVariableTarget.Process)
{
return Environment.GetEnvironmentVariable(name, scope);
}
var value = EnvironmentHelper.GetVariable(cmdlet, name, scope); Defensive patterns
Strategy: validation
Validate before calling
// Only call registry-backed helpers for User/Machine; handle Process separately.
if (scope == EnvironmentVariableTarget.Process)
{
var v = Environment.GetEnvironmentVariable(name, scope);
return v ?? string.Empty;
}
if (scope != EnvironmentVariableTarget.User && scope != EnvironmentVariableTarget.Machine)
{
throw new ArgumentOutOfRangeException(nameof(scope), scope, "Only User, Machine, Process are supported.");
}
return EnvironmentHelper.GetVariable(cmdlet, name, scope); Type guard
static bool IsRegistryBackedScope(EnvironmentVariableTarget scope)
=> scope == EnvironmentVariableTarget.User || scope == EnvironmentVariableTarget.Machine; Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- .NET 4.8 is not installed or may need a reboot to complete i
- The '{0}' source does not support searching for packages
- The '{0}' source does not support installing packages
- The '{0}' source does not support upgrading packages
- The '{0}' source does not support uninstalling packages
AI-assisted analysis of chocolatey/choco@0d5abdd10c (2026-08-13).
Data as JSON: /api/errors/e41feced5cc21885.
Report an issue: GitHub.