Source/Environment/Test-EnvironmentVariable.ps1
|
<# .SYNOPSIS Checks whether an environment variable exists and is not empty. .DESCRIPTION Reads an environment variable via Get-EnvironmentVariable and checks whether it's set to a non-empty value in the given scope. .PARAMETER Name The name of the environment variable. .PARAMETER Scope The scope to check the variable in: Process, User, or Machine. Defaults to Process. .EXAMPLE Test-EnvironmentVariable -Name "Foo" -Scope "User" .EXAMPLE "Foo", "Bar" | Test-EnvironmentVariable -Scope "User" .INPUTS [System.String] The variable name. Accepts pipeline input by value or by property name. .OUTPUTS [System.Boolean] True if the variable exists and isn't empty, otherwise False. #> function Test-EnvironmentVariable { [OutputType([bool])] [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Name, [System.EnvironmentVariableTarget] $Scope = [System.EnvironmentVariableTarget]::"Process" ) process { $value = Get-EnvironmentVariable -Name $Name -Scope $Scope return -not [string]::IsNullOrEmpty($value) } } |