-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment.ps1
203 lines (155 loc) · 5.68 KB
/
environment.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
function Get-EnvironmentVariable() {
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true)]
[string] $Name,
[Parameter(Mandatory = $true, ParameterSetName = "Machine")]
[switch] $Machine,
[Parameter(Mandatory = $true, ParameterSetName = "User")]
[switch] $User,
[Parameter(Mandatory = $false, ParameterSetName = "Effective")]
[switch] $Effective
)
$value =
if ($Machine) {
[Environment]::GetEnvironmentVariable($Name, [System.EnvironmentVariableTarget]::Machine)
}
elseif ($User) {
[Environment]::GetEnvironmentVariable($Name, [System.EnvironmentVariableTarget]::User)
}
else {
(Get-Item env:$Name -ErrorAction SilentlyContinue)?.Value
}
if (! $value) {
Write-Error "Environment variable '$Name' not found."
}
return $value
<#
.SYNOPSIS
Returns the value of an environment variable.
.DESCRIPTION
Returns the value of the specified environment variable,
either in the machine environment, the user environment, or the value in effect.
.PARAMETER Name
The name of the environment variable.
.PARAMETER Machine
If specified, the value of the environment variable in the machine environment is returned.
.PARAMETER User
If specified, the value of the environment variable in the user environment is returned.
.PARAMETER Effective
If specified, the value of the environment variable is returned which is in effect. (Default.)
.OUTPUTS string - The value of the environment variable.
.ALIAS getenv
.EXAMPLE
Get-EnvironmentVariable "TEMP"
.EXAMPLE
Get-EnvironmentVariable "TEMP" -Effective
.EXAMPLE
Get-EnvironmentVariable "TEMP" -Machine
.EXAMPLE
Get-EnvironmentVariable -Name "TEMP"
.EXAMPLE
Get-EnvironmentVariable -Name "TEMP" -User
#>
}
function Set-EnvironmentVariable() {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Position = 0, Mandatory = $true)]
[string] $Name,
[Parameter(Position = 1, Mandatory = $true)]
[string] $Value,
[Parameter(Mandatory = $false, ParameterSetName = "Machine")]
[switch] $Machine,
[Parameter(Mandatory = $true, ParameterSetName = "User")]
[switch] $User
)
try {
if ($User) {
$environment = [System.EnvironmentVariableTarget]::User
$envType = "User"
}
# Machine is the default
else {
Assert-Administrator
$environment = [System.EnvironmentVariableTarget]::Machine
$envType = "Machine"
}
if ($PSCmdlet.ShouldProcess($Name, "Set environment variable in ${envType} environment")) {
[Environment]::SetEnvironmentVariable($Name, $Value, $environment)
}
}
catch {
Write-Error "$($_.Exception.Message) Trying to set a machine environment variable."
}
<#
.SYNOPSIS
Sets the value of an environment variable.
.DESCRIPTION
Sets the value of the specified environment variable,
either in the machine environment or the user environment.
.PARAMETER Name
The name of the environment variable.
.PARAMETER Value
The value to set the environment variable to.
.PARAMETER Machine
If specified, the environment variable is set in the machine environment.
.PARAMETER User
If specified, the environment variable is set in the user environment.
.ALIAS setenv
.EXAMPLE
Set-EnvironmentVariable -Name "JAVA_HOME" -Value "C:\Java\JDK" -Machine
.EXAMPLE
Set-EnvironmentVariable -Name "GOPATH" -Value "C:\Go\GOPATH" -User
#>
}
function Remove-EnvironmentVariable() {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Position = 0, Mandatory = "true")]
[string] $Name,
[Parameter(Mandatory = $false, ParameterSetName = "Machine")]
[switch] $Machine,
[Parameter(Mandatory = $true, ParameterSetName = "User")]
[switch] $User
)
try {
if ($User) {
$environment = [System.EnvironmentVariableTarget]::User
$envType = "User"
}
# Machine is default
else {
Assert-Administrator
$environment = [System.EnvironmentVariableTarget]::Machine
$envType = "Machine"
}
if ($PSCmdlet.ShouldProcess($Name, "Remove environment variable from ${envType} environment")) {
[Environment]::SetEnvironmentVariable($Name, $null, $environment)
}
}
catch {
Write-Error "$($_.Exception.Message) Trying to remove a machine environment variable."
}
<#
.SYNOPSIS
Removes an environment variable.
.DESCRIPTION
Removes the specified environment variable,
either from the machine environment or the user environment.
.PARAMETER Name
The name of the environment variable.
.PARAMETER Machine
If specified, the environment variable is removed from the machine environment.
.PARAMETER User
If specified, the environment variable is removed from the user environment.
.ALIAS rmenv
.EXAMPLE
Remove-EnvironmentVariable -Name "JAVA_HOME" -Machine
.EXAMPLE
Remove-EnvironmentVariable -Name "GOPATH" -User
#>
}
New-Alias -Name getenv -Value Get-EnvironmentVariable -ErrorAction SilentlyContinue | Out-Null
New-Alias -Name setenv -Value Set-EnvironmentVariable -ErrorAction SilentlyContinue | Out-Null
New-Alias -Name rmenv -Value Remove-EnvironmentVariable -ErrorAction SilentlyContinue | Out-Null