1

If i enter:

set DOTNET_CLI_TELEMETRY_OPTOUT=1

or

setx DOTNET_CLI_TELEMETRY_OPTOUT 1

to save this setting permanently, then the setting DOTNET_CLI_TELEMETRY_OPTOUT should be set to True, but how do i query it?

There is no

get DOTNET_CLI_TELEMETRY_OUTPUT

command. And

echo $DOTNET_CLI_TELEMETRY_OPTOUT

does only give a 1, if

set DOTNET_CLI_TELEMETRY_OPTOUT 1

was entered instead of

set DOTNET_CLI_TELEMETRY_OPTOUT=1

that should be used.

Coder
  • 197
  • 6

1 Answers1

1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thank you very much, that solved it. Yesterday before I asked her my question i used this [link]https://stackoverflow.com/questions/39306618/how-do-i-set-the-value-in-a-command-shell-for-dotnet-core[/link] answer, but it was insufficient. – Coder Apr 15 '23 at 14:25
  • 1
    Glad to hear it, @Coder; my pleasure. Yes, the linked answer is focused on `cmd.exe` and other shells, not PowerShell. – mklement0 Apr 15 '23 at 14:36