Public/Manage-SpecEnvironmentVariable.ps1
Function Manage-SpecEnvironmentVariable { <# .SYNOPSIS Manages environment variables by adding, replacing, or deleting them at both the Process and Machine levels. .DESCRIPTION The Manage-SpecEnvironmentVariable function allows you to manage environment variables by performing actions such as adding, replacing, or deleting them. The function supports three actions: "Add" (adds a new variable), "Replace" (replaces an existing variable), and "Delete" (removes an existing variable). It operates at both the Process and Machine levels. THis function was written for a specific use case, where both machine and process level env vars must be actioned at once. .PARAMETER VariableName Specifies the name of the environment variable to manage. .PARAMETER Value Specifies the value to set for the environment variable. Required when the Action is "Add" or "Replace". .PARAMETER Action Specifies the action to perform. Valid values are "Add" (adds a new variable), "Replace" (replaces an existing variable), or "Delete" (removes an existing variable). .EXAMPLE Manage-SpecEnvironmentVariable -VariableName "SampleVar" -Value "SampleValue" -Action "Add" Adds a new environment variable named "SampleVar" with the value "SampleValue" at both Process and Machine levels. If the variable already exists it will not add the new environment variable. .EXAMPLE Manage-SpecEnvironmentVariable -VariableName "SampleVar" -Value "NewValue" -Action "Replace" Replaces the value of the existing environment variable named "SampleVar" with "NewValue" at both Process and Machine levels. .EXAMPLE Manage-SpecEnvironmentVariable -VariableName "SampleVar" -Action "Delete" Deletes the environment variable named "SampleVar" at both Process and Machine levels. .NOTES Author : owen.heaume Version : 1.0 #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [AllowNull()] [AllowEmptyString()] [string]$VariableName, [Parameter(Mandatory = $false)] [AllowNull()] [AllowEmptyString()] [string]$Value, [validateset ("Add", "Replace", "Delete") ] [string]$Action ) if ($action -eq 'Add') { write-host "Adding env var: [$VariableName] with value [$Value]" -ForegroundColor DarkCyan $result = Get-SpecEnvironmentVariable -VariableName $VariableName -scope Process if ($result -eq $false) { write-host "Adding at the [Process] level" -ForegroundColor DarkGray $procresult = Set-SpecEnvironmentVariable -VariableName $VariableName -VariableValue $Value -scope Process write-host "Adding at the [Machine] level" -ForegroundColor DarkGray $machresult = Set-SpecEnvironmentVariable -VariableName $VariableName -VariableValue $Value -scope machine write-host "Process complete" -ForegroundColor DarkGreen } else { write-host "The variable already exists and has the value: [$result] so not adding. To overwrite the value, please use 'Replace' as the action." -ForegroundColor DarkYellow } } if ($action -eq 'Replace') { write-host "Replacing env var: [$VariableName] with value [$Value]" -ForegroundColor DarkCyan write-host "Replacing at the [Process] level" -ForegroundColor DarkGray $procresult = Set-SpecEnvironmentVariable -VariableName $VariableName -VariableValue $Value -scope Process write-host "Replacing at the [Machine] level" -ForegroundColor DarkGray $machresult = Set-SpecEnvironmentVariable -VariableName $VariableName -VariableValue $Value -scope machine write-host "Process complete" -ForegroundColor DarkGreen } if ($action -eq 'Delete') { write-host "Deleting env var: [$VariableName]" -ForegroundColor DarkCyan $result = Get-SpecEnvironmentVariable -VariableName $VariableName -scope Process if ($result -eq $false) { write-host "The env var does not exist to delete" -ForegroundColor DarkGray } else { write-host "Deleting at the [Process] level" -ForegroundColor DarkGray $procresult = Remove-SpecEnvironmentVariable -VariableName $VariableName -scope Process write-host "Deleting at the [Machine] level" -ForegroundColor DarkGray $machresult = Remove-SpecEnvironmentVariable -VariableName $VariableName -scope machine write-host "Process complete" -ForegroundColor DarkGreen } } } |