Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active July 5, 2024 23:13
Show Gist options
  • Save jdhitsolutions/43f16446e3ad73bcd237faf38764ddfc to your computer and use it in GitHub Desktop.
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