tl;dr
Use $env:DOTNET_CLI_TELEMETRY_OPTOUT
to get the current process' value of environment variable DOTNET_CLI_TELEMETRY_OPTOUT
(you can't tell from this value whether it reflects a persistently defined environment variable or one that was set for the current process only).
Use, e.g. $env:DOTNET_CLI_TELEMETRY_OPTOUT = 1
to set its value, but it will be in effect for the current process only.
To persistently set a value (which is only supported on Windows), use setx.exe
(as you tried) or use .NET APIs (see below).
- Such persistent updates are only visible to future sessions (as process-level environment variables).
- However, you can query the currently persisted value via .NET APIs (see below).
As for what you tried:
set DOTNET_CLI_TELEMETRY_OPTOUT=1
You're trying to use cmd.exe
syntax in PowerShell, which won't work.
In PowerShell, the above translates to the following, given that set
is a built-in alias of the Set-Variable
, whose first positional parameter is -Name
Set-Variable -Name DOTNET_CLI_TELEMETRY_OPTOUT=1
This creates a shell-only variable literally named DOTNET_CLI_TELEMETRY_OPTOUT=1
without a value ($null
).
By shell-only variable I mean a regular PowerShell variable that is known to the current PowerShell session only, and - unlike environment variables - is not seen by other processes.
To define an environment variable for the current process, use the $env:
namespace:
$env:DOTNET_CLI_TELEMETRY_OPTOUT = 1
You can query the value with the same syntax, which works equally for persistent environment variables and those defined only for the current process:
$env:DOTNET_CLI_TELEMETRY_OPTOUT
PowerShell offers no direct support for defining persistent environment variables.
Using the setx.exe
utility (on Windows) - as you tried - is an option.
You can also use .NET APIs:
# 'User' creates / updates a persistent definition for the *current user*.
# Use 'Machine' to create / update a machine-level definition, but
# note that doing so requires *elevation* (running as admin).
[Environment]::SetEnvironmentVariable('DOTNET_CLI_TELEMETRY_OPTOUT', '1', 'User')
Note, however, that in both cases the new / updated value will only be seen in future sessions, i.e. will only automatically surface as process-level environment variables there.
You can, however, use .NET APIs again to retrieve the currently persisted value directly from the registry, assuming you know its scope (User
vs. Machine
- you may have to try both); e.g.:
[Environment]::GetEnvironmentVariable('DOTNET_CLI_TELEMETRY_OPTOUT', 'User')
Note:
Neither of the two persistent-update methods are suitable for updating REG_EXPAND_SZ
-based persistent environment-variable definitions, i.e. those expressed in terms of other environment variables.
Notably, this means that the PATH
environment variable shouldn't be updated this way - see this answer for a solution.