public/Set-EnvironmentVariable.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<#
    .Synopsis
        Set a value as an environment variable.
    .Description
        Set a value as an environment variable.
    .Example
        PS C:\> Set-EnvironmentVariable -Name SECRET -Value Sauce
        Sets '$env:SECRET' in the current environment session.
#>

function Set-EnvironmentVariable {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
        # The name of the environment vairable to set.
        [Parameter(Mandatory = $true)]
        [String]
        $Name,
        # The value of the environment vairable to set.
        [Parameter(Mandatory = $true)]
        [String]
        $Value
    )

    begin {
        Write-LogMessage -Message "Started execution"
    }

    process {
        if ($PSCmdlet.ShouldProcess("Environment", "Adding '$Name'")) {
            [Environment]::SetEnvironmentVariable("$Name", "$Value")
        }
    }

    end {
        Write-LogMessage -Message "Finished execution"
    }
}