38 lines
1.2 KiB
PowerShell
38 lines
1.2 KiB
PowerShell
function Remove-EnvironmentVariable {
|
|
<#
|
|
.SYNOPSIS
|
|
Removes an environment variable. Must supply the store name, defaults to machine level.
|
|
|
|
.PARAMETER Name
|
|
Removes the environment variable with the specified name.
|
|
|
|
.PARAMETER StoreName
|
|
The appropriate store to put the variable in. Defaults to machine.
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[ValidateNotNullOrEmpty()]
|
|
[Alias("Key")]
|
|
[string]$Name,
|
|
[Parameter(Mandatory = $true)]
|
|
[Alias("Store")]
|
|
[Alias("Location")]
|
|
[ValidateSet("Process","User","Machine")]
|
|
[string]$StoreName
|
|
)
|
|
|
|
$logLead = (Get-LogLeadName)
|
|
|
|
# Setting the value to $null deletes it from the store
|
|
$Value = $null
|
|
|
|
Write-Host "$logLead : Remove environment variable [$Name] in [$StoreName] store"
|
|
|
|
[System.Environment]::SetEnvironmentVariable($Name, $Value, [System.EnvironmentVariableTarget]::$StoreName)
|
|
|
|
if ($StoreName -ne "Process") {
|
|
Write-Warning "$logLead : Environment variable with name [$Name] removed form [$StoreName] but not from the [Process] store.`r`nThis value may still be visible to the executing process."
|
|
}
|
|
}
|