Public/Get-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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<#
.SYNOPSIS
A PowerShell module to handle environment variables, supporting variable expansion. This function handles GETTING environment variables.
 
.DESCRIPTION
This module is capable of Retrieving Environment Variables in any scope (Process, User, Machine). It will return the value of the Envrionment Variable.
 
.PARAMETER Name
[String] Specify the name of the Environment Variable to retrieve.
 
.PARAMETER Scope
[System.EnvironmentVariableTarget], [String]
Specify the scope to search for the target Environment Variable.
 
Process : Environment Variables in the running process.
 
User : Environment Variables in the User Scope affect the Global Environment Variables for the current user.
 
Machine : Environment Variables in the Machine Scope change the settings in the registry for all users.
 
.PARAMETER Expanded
[Switch]
If enabled any Environment Variables in the output of the command will be expanded.
String : A simple string value
ExpandString : A string value which contains unexpanded environment variables in the syntax of %VARIABLENAME%. These variables will NOT be expanded when the value is read.
 
.PARAMETER ShowProperties
[Switch] If enabled this parameter will show the Name, Value, Scope, ValueType and (if a String containing an unexpanded Environment Variable) the BeforeExpansion properties and their respective values. Otherwise only the Value will be output as a string.
 
.EXAMPLE
PS C:\> Get-EnvironmentVariable -name TestVar -Scope Machine -ShowProperties
 
Name : TestVar
Value : TestValue
Scope : Machine
ValueType : String
BeforeExpansion : TestValue
 
.EXAMPLE
PS C:\> Get-EnvironmentVariable -name TestPathVar -Scope Machine -ShowProperties
 
Name : TestPathVar
Value : C:\Users\rblea\AppData\Local\Temp\TestValue2
Scope : Machine
ValueType : String
BeforeExpansion : %TEMP%\TestValue2
 
.EXAMPLE
PS C:\> Get-EnvironmentVariable -name TestPathVar -Scope Machine
C:\Users\USER\AppData\Local\Temp\TestValue2
 
.EXAMPLE
PS C:\> Get-EnvironmentVariable -name TestPathVar -Scope Machine -Expanded
%TEMP%\TestValue2
 
 
.INPUTS
[String],[System.EnvironmentVariableTarget],[Boolean]
 
.OUTPUTS
[Hashtable],[String]
 
.NOTES
 
.LINK
https://github.com/rbleattler/xEnvironmentVariables
 
#>

function Get-EnvironmentVariable {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory)]
        [String]
        $Name,
        [Parameter()]
        [System.EnvironmentVariableTarget]
        $Scope = [System.EnvironmentVariableTarget]::Process,
        [Parameter()]
        [Switch]
        $Expanded,
        [Switch]
        $ShowProperties
    )
    begin {
    }
    process {
        $Getter = [System.Environment]::GetEnvironmentVariable($Name, $Scope)

        if ($null -eq $Getter) {
            $RawValue = $null
            $GetterType = $null
        } else {
            if ($Scope -ne "Process") {
                if (!$Expanded) {
                    $AllEnvironmentVariables = Get-Item -Path (Get-EnvironmentPath -Scope $Scope)
                    $GetterType = $AllEnvironmentVariables.GetValueKind($Name)
                } else {
                    $AllEnvironmentVariables = [System.Environment]::GetEnvironmentVariables($Scope)
                    $GetterType = $Getter.GetTypeCode()
                }
                if ($GetterType -eq "ExpandString") {
                    $RawValue = $AllEnvironmentVariables.GetValue(
                        $Name, $null, 'DoNotExpandEnvironmentNames'
                    )
                } elseif ($GetterType -eq "String") {
                    $RawValue = $Getter
                    if ($Expanded) {
                        $Getter = [System.Environment]::ExpandEnvironmentVariables($Getter)
                    }
                } else {
                    # inappropriate kind (dword, bytes, ...)
                    $RawValue = $null
                    $GetterType = $null
                }
            } else {
                # $Scope -eq "Process"
                $RawValue = $null
                $GetterType = "String"
            }
        }
        $params = @{
            Name            = $Name
            Value           = $Getter
            Scope           = $Scope
            ValueType       = $GetterType
            BeforeExpansion = $RawValue
        }
        $null = New-EnvironmentVariableObject @params | Set-Variable -Name NewEnvVar

        if ($ShowProperties) {
            $NewEnvVar | Add-Member ScriptMethod ToString { $this.Value } -Force -PassThru
        } else {
            if (!$Expanded) {
                $NewEnvVar | Add-Member ScriptMethod ToString { $this.Value } -Force -PassThru | Select-Object -ExpandProperty BeforeExpansion
            } else {
                $NewEnvVar | Add-Member ScriptMethod ToString { $this.Value } -Force -PassThru | Select-Object -ExpandProperty Value
            }
            #$NewEnvVar.Value | Add-Member ScriptMethod ToString { $this.Value } -Force -PassThru
        }
    }

    end {
    }

}