Skip to content

Instantly share code, notes, and snippets.

@asheroto
Last active January 11, 2024 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asheroto/56d31c741792466b7fbfacc5d47e0e74 to your computer and use it in GitHub Desktop.
Save asheroto/56d31c741792466b7fbfacc5d47e0e74 to your computer and use it in GitHub Desktop.
Scripts to disable PowerShell Telemetry and Update Check. Scripts add the System environment variables.
# Disables PowerShell telemetry by setting the environment variable associated with it.
# More info: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_telemetry
#Requires -RunAsAdministrator
# See this for more info:
# https://gist.github.com/asheroto/5c1711fcbd888c5a621b1ff0b91dfc00
# Bypasses the typical delay experienced with Environment.SetEnvironmentVariable, which can be slow due to its broadcasting a message to all top-level windows.
# These functions offer a more efficient alternative for getting, setting, or deleting environment variables.
function Set-EnvironmentVariable {
<#
.SYNOPSIS
Instantly sets an environment variable in the machine or user environment by updating the registry.
.DESCRIPTION
Instantly sets an environment variable in the machine or user environment by updating the registry.
.PARAMETER Name
The name of the environment variable to set.
.PARAMETER Value
The value of the environment variable to set.
.PARAMETER Target
The target environment to set the environment variable in. Valid values are 'Machine' and 'User'.
.EXAMPLE
Set-EnvironmentVariable -Name "MyVariable" -Value "MyValue" -Target "Machine"
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Value,
[Parameter(Mandatory = $true)]
[ValidateSet("Machine", "User")]
[string]$Target
)
$regKey = switch ($Target.ToLower()) {
"machine" { 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' }
"user" { 'HKCU:\Environment' }
}
Set-ItemProperty -Path $regKey -Name $Name -Value $Value
}
# Add POWERSHELL_TELEMETRY_OPTOUT environment variable
Write-Output "Adding POWERSHELL_TELEMETRY_OPTOUT environment variable"
Set-EnvironmentVariable -Name "POWERSHELL_TELEMETRY_OPTOUT" -Value "1" -Target "Machine"
Write-Output "Environment variable added successfully."
# Disables PowerShell update check that is performed when PowerShell is launched.
# More info: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_update_notifications
#Requires -RunAsAdministrator
# See this for more info:
# https://gist.github.com/asheroto/5c1711fcbd888c5a621b1ff0b91dfc00
# Bypasses the typical delay experienced with Environment.SetEnvironmentVariable, which can be slow due to its broadcasting a message to all top-level windows.
# These functions offer a more efficient alternative for getting, setting, or deleting environment variables.
function Set-EnvironmentVariable {
<#
.SYNOPSIS
Instantly sets an environment variable in the machine or user environment by updating the registry.
.DESCRIPTION
Instantly sets an environment variable in the machine or user environment by updating the registry.
.PARAMETER Name
The name of the environment variable to set.
.PARAMETER Value
The value of the environment variable to set.
.PARAMETER Target
The target environment to set the environment variable in. Valid values are 'Machine' and 'User'.
.EXAMPLE
Set-EnvironmentVariable -Name "MyVariable" -Value "MyValue" -Target "Machine"
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Value,
[Parameter(Mandatory = $true)]
[ValidateSet("Machine", "User")]
[string]$Target
)
$regKey = switch ($Target.ToLower()) {
"machine" { 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' }
"user" { 'HKCU:\Environment' }
}
Set-ItemProperty -Path $regKey -Name $Name -Value $Value
}
# Add POWERSHELL_UPDATECHECK environment variable
Write-Output "Adding POWERSHELL_UPDATECHECK environment variable"
Set-EnvironmentVariable -Name "POWERSHELL_UPDATECHECK" -Value "Off" -Target "Machine"
Write-Output "Environment variable added successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment