Public/Set-EnvironmentVariable.ps1
<#
.SYNOPSIS Updates or renames an environment variable specified by name, type, data and previous data. .DESCRIPTION Updates or Renames an environment variable specified by name, type, data and previrous data. .NOTES This function is pulled directly from the real Microsoft Windows Admin Center PowerShell scripts use rights (according to Microsoft): We grant you a non-exclusive, royalty-free right to use, modify, reproduce, and distribute the scripts provided herein. ANY SCRIPTS PROVIDED BY MICROSOFT ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS OR A PARTICULAR PURPOSE. .ROLE Administrators .PARAMETER oldName This parameter is MANDATORY. TODO .PARAMETER newName This parameter is MANDATORY. TODO .PARAMETER value This parameter is MANDATORY. TODO .PARAMETER type This parameter is MANDATORY. TODO .EXAMPLE # Open an elevated PowerShell Session, import the module, and - PS C:\Users\zeroadmin> Set-EnvironmentVariable -oldName "OldVar" -newName "NewVar" -value "thing1" -type "User" #> function Set-EnvironmentVariable { param( [Parameter(Mandatory = $True)] [String] $oldName, [Parameter(Mandatory = $True)] [String] $newName, [Parameter(Mandatory = $True)] [String] $value, [Parameter(Mandatory = $True)] [String] $type ) Set-StrictMode -Version 5.0 $nameChange = $false if ($newName -ne $oldName) { $nameChange = $true } If (-not [Environment]::GetEnvironmentVariable($oldName, $type)) { @{ Status = "currentMissing" } return } If ($nameChange -and [Environment]::GetEnvironmentVariable($newName, $type)) { @{ Status = "targetConflict" } return } If ($nameChange) { [Environment]::SetEnvironmentVariable($oldName, $null, $type) [Environment]::SetEnvironmentVariable($newName, $value, $type) @{ Status = "success" } } Else { [Environment]::SetEnvironmentVariable($newName, $value, $type) @{ Status = "success" } } } |