Public/Get-EnvironmentVariables.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<#
.SYNOPSIS
A PowerShell module to handle environment variables, supporting variable expansion. This function handles GETTING ALL environment variables.
 
.DESCRIPTION
This module is capable of Retrieving Environment Variables in any scope (Process, User, Machine).
 
.PARAMETER Names
[switch] Returns only the Names of the environment variables.
 
.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 OutputType
[String]
Specify the output type for the command.
 
"String" : Returns the value as a simple string of values separated by spaces.
"Array" : Returns an array of string values
"CSV" : Returns a Two line CSV (Comma Delimited)
"Object" : Returns a PSCustomObject with Variable names as Object Properties whose values are those of the Environment Variables
"JSON" : Returns a JSON output where each node contains a 'Name' and 'Value' for one Environment Variable.
 
.EXAMPLE
Get Environment Variables in the Process Scope
 
PS C:\> Get-EnvironmentVariables
USERDOMAIN : ComputerDomain
COLORTERM : truecolor
ComSpec : C:\WINDOWS\system32\cmd.exe
TERM_PROGRAM_VERSION : 1.40.0
TestPathVar2 : C:\WINDOWS\TEMP\TestValue2
CommonProgramFiles : C:\Program Files\Common Files
NUMBER_OF_PROCESSORS : 12
...
.EXAMPLE
Get Environment Variables in the Machine Scope
 
PS C:\> Get-EnvironmentVariables -Scope Machine
PROCESSOR_LEVEL : 23
TestPathVar2 : C:\Users\USER\AppData\Local\Temp\TestValue2
USERNAME : SYSTEM
PROCESSOR_ARCHITECTURE : AMD64
TestVar : TestValue
NUMBER_OF_PROCESSORS : 12
PROCESSOR_REVISION : 0802
...
 
.EXAMPLE
PS C:\> Get-EnvironmentVariables -OutputType JSON
[
  {
    "Name": "PROCESSOR_IDENTIFIER",
    "Value": "AMD64 Family 23 Model 8 Stepping 2, AuthenticAMD"
  },
  {
    "Name": "PROCESSOR_ARCHITECTURE",
    "Value": "AMD64"
  },
  {
    "Name": "ProgramData",
    "Value": "C:\\ProgramData"
  },
  {
    "Name": "OS",
    "Value": "Windows_NT"
  },
  {
    "Name": "TestValue2",
    "Value": "%TMP%\\TestValue3"
  },
  {
    "Name": "USERPROFILE",
    "Value": "C:\\Users\\USERNAME"
  },
  {
    "Name": "ALLUSERSPROFILE",
    "Value": "C:\\ProgramData"
  },
  {
    "Name": "CommonProgramW6432",
    "Value": "C:\\Program Files\\Common Files"
  },
  {
    "Name": "TestPathVar2",
    "Value": "C:\\WINDOWS\\TEMP\\TestValue2"
  },
  {
    "Name": "PROCESSOR_LEVEL",
    "Value": "23"
  },
  {
    "Name": "ProgramFiles(x86)",
    "Value": "C:\\Program Files (x86)"
  },
  {
    "Name": "ProgramW6432",
    "Value": "C:\\Program Files"
  },
  {
    "Name": "HOMEDRIVE",
    "Value": "C:"
  },
...
]
.EXAMPLE
PS C:\> Get-EnvironmentVariables -OutputType CSV
PROCESSOR_IDENTIFIER,PROCESSOR_ARCHITECTURE,ProgramData,OS,TestValue2...
"AMD64 Family 23 Model 8 Stepping 2, AuthenticAMD",AMD64,C:\ProgramData,Windows_NT,%TMP%\TestValue3...
 
.EXAMPLE
PS C:\> Get-EnvironmentVariables -OutputType Array
Name Value
---- -----
PROCESSOR_IDENTIFIER AMD64 Family 23 Model 8 Stepping 2, AuthenticAMD
PROCESSOR_ARCHITECTURE AMD64
ProgramData C:\ProgramData
OS Windows_NT
TestValue2 %TMP%\TestValue3
...
 
.EXAMPLE
PS C:\> Get-EnvironmentVariables -OutputType Array -Names
 
Name
----
PROCESSOR_IDENTIFIER
PROCESSOR_ARCHITECTURE
ProgramData
OS
TestValue2
DriverData
ComSpec
USERPROFILE
ALLUSERSPROFILE
LOGONSERVER
USERDOMAIN_ROAMINGPROFILE
TERM_PROGRAM_VERSION
PSExecutionPolicyPreference
...
 
.INPUTS
[String]
 
.OUTPUTS
[Hashtable],[String],[JSON],[CSV],[PSCustomObject]
 
.NOTES
 
.LINK
https://github.com/rbleattler/xEnvironmentVariables
 
#>

function Get-EnvironmentVariables {
    param (
        [Parameter()]
        [System.EnvironmentVariableTarget]
        [System.String]
        $Scope = [System.EnvironmentVariableTarget]::Process,
        [Parameter()]
        [Switch]
        $Names,
        [Parameter()]
        [Switch]
        $Values,
        [Parameter()]
        [ValidateSet("String", "Array", "CSV", "Object", "JSON")]
        [String]
        $OutputType = "Object"
    )
    if ($Scope.GetTypeCode() -eq 'String') {
        $Scope = [System.EnvironmentVariableTarget]::$Scope
    }
    try {
        $EnvironmentVariables = [System.Environment]::GetEnvironmentVariables($Scope)

        if ((!$Names -and !$Values) -or ($Names -and $Values)) {
            $ConversionOutputType = 'Both'
        } elseif (!$Values -and $Names) {
            $ConversionOutputType = 'Name'
        } elseif (!$Names -and $Values) {
            $ConversionOutputType = 'Value'
        } else {
            throw "Invalid Selection"
        }

        Convert-KeyValuePair -InputObject $EnvironmentVariables -OutputType $OutputType -OutputElements $ConversionOutputType

    } catch {
        $ScriptName = $_.InvocationInfo.ScriptName
        $Position = 'In {2} At Line: {0} Char: {1}' -f $_.InvocationInfo.ScriptLineNumber, $_.InvocationInfo.OffsetInLine, $ScriptName
        $Category = $_.CategoryInfo.Category
        Write-Error -Message "Error was `"$_`"" -Category $Category -CategoryTargetName $Position
    }




}