Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active November 10, 2023 13:08
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jdhitsolutions/43f16446e3ad73bcd237faf38764ddfc to your computer and use it in GitHub Desktop.
Set a temporary environment variable in PowerShell
#requires -version 5.1
#set a temporary environment variable
# example using the function alias and positional parameters: se rust_log debug
Function Set-EnvironmentVariable {
[CmdletBinding(SupportsShouldProcess)]
[alias("se")]
Param (
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[Parameter(Position = 1, Mandatory)]
[object]$Value,
[switch]$Passthru
)
$splat = @{
Path = (Join-Path -Path env: -ChildPath $name)
Value = $Value
Passthru = $Passthru
Force = $true
}
Set-Item @splat
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment