Get-EnvironmentVariable

Get-EnvironmentVariable

Get-EnvironmentVariable

SYNOPSIS

Gets an Environment Variable.

SYNTAX

Get-EnvironmentVariable [-Name] <String> [-Scope] <EnvironmentVariableTarget> [-PreserveVariables]
 [-IgnoredArguments <Object[]>] [<CommonParameters>]

DESCRIPTION

This will will get an environment variable based on the variable name and scope while accounting whether to expand the variable or not (e.g.: %TEMP%-> C:\User\Username\AppData\Local\Temp).

EXAMPLES

Example 1

PS C:\> Get-EnvironmentVariable -Name 'TEMP' -Scope User -PreserveVariables

Gets the TEMP encironment variable from the user scope, without expanding embedded environment variables.

Example 2

PS C:\> Get-EnvironmentVariable -Name 'PATH' -Scope Machine

Gets the PATH environment variable from the machine scope, expanding embedded environment variables.

PARAMETERS

-IgnoredArguments

Allows splatting with arguments that do not apply. Do not use directly.

Type: Object[]
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Name

The environment variable you want to get the value from.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-PreserveVariables

Whether you want to expand embedded environment variable names or not. When not set, variables will be expanded.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Scope

The environment variable target scope. This is Process, User, or Machine.

Type: EnvironmentVariableTarget
Parameter Sets: (All)
Aliases:
Accepted values: Process, User, Machine

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

System.String

Outputs the value of the target environment variable as a string.

NOTES

This helper reduces the number of lines one would have to write to get environment variables, mainly when not expanding the variables is a must.

Get-EnvironmentVariableNames Set-EnvironmentVariable Cmdlet Reference Function Reference