Create a non-path environment variable

Description

This example shows how you can use the Environment resource to ensure a non-path environment variable exists with a specific value.

With Ensure set to Present, Name set to TestEnvironmentVariable, and Value set to TestValue, the resource adds an environment variable called TestEnvironmentVariable with the value TestValue if it doesn't exist.

With Path set to $false, if TestEnvironmentVariable exists with any value other than TestValue, the resource sets it to exactly TestValue.

With Target set to an array with both Process and Machine, the resource creates or sets the environment variable in both the process and machine targets.

With Invoke-DscResource

This script shows how you can use the Environment resource with the Invoke-DscResource cmdlet to ensure TestEnvironmentVariable is set in the process and machine targets as TestValue.

[CmdletBinding()]
param()

begin {
    $SharedParameters = @{
        Name       = 'Environment'
        ModuleName = 'PSDscResource'
        Properties = @{
            Name   = 'TestEnvironmentVariable'
            Value  = 'TestValue'
            Ensure = 'Present'
            Path   = $false
            Target = @(
                'Process'
                'Machine'
            )
        }
    }

    $NonGetProperties = @(
        'Value'
        'Path'
        'Ensure'
    )
}

process {
    $TestResult = Invoke-DscResource -Method Test @SharedParameters

    if ($TestResult.InDesiredState) {
        $QueryParameters = $SharedParameters.Clone()

        foreach ($Property in $NonGetProperties) {
            $QueryParameters.Properties.Remove($Property)
        }

        Invoke-DscResource -Method Get @QueryParameters
    } else {
        Invoke-DscResource -Method Set @SharedParameters
    }
}

With a Configuration

This snippet shows how you can define a Configuration with an Environment resource block to ensure TestEnvironmentVariable is set in the process and machine targets as TestValue.

Configuration CreateNonPathVariable  {
    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost {
        Environment ExampleEnvironment {
            Name   = 'TestEnvironmentVariable'
            Value  = 'TestValue'
            Ensure = 'Present'
            Path   = $false
            Target = @(
                'Process'
                'Machine'
            )
        }
    }
}