forked from bcdady/MyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-EnvVariable.ps1
79 lines (71 loc) · 2.84 KB
/
Set-EnvVariable.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
#requires -version 3.0
function Set-EnvVariable {
<#
.SYNOPSIS
Sets the value of an environment variable
.DESCRIPTION
This function will set the value of an environment variable. The default behavior is to overwrite the existing value. The concatenate switch will append the new value to the existing value.
.EXAMPLE
Set-EnvVariable -Name Path -Value C:\git -Concatenate
Adds, ";c:\git" to the path variable
.EXAMPLE
(get-childitem c:\ -recurse -filter git.exe -force).fullname | split-path | Foreach {Set-EnvVariable -Name -value $_}
Adds all folders containing git.exe to the Path
.EXAMPLE
Set-EnvVariable -Name Path -Value $NewPath
Set the Path to the content of $NewPath
.NOTES
Written by Jason Morgan
Created on 6/15/2014
Last Modified 6/2/2014
.LINK
https://gallery.technet.microsoft.com/Set-Environment-Variable-0e7492a3
#>
[cmdletbinding(SupportsShouldProcess=$true,
ConfirmImpact='high',
DefaultParameterSetName='Default')]
Param (
# Set the value of the environment variable
[Parameter(Mandatory,
ValueFromPipeline,
ParameterSetName='Default')]
[Parameter(ParameterSetName='Concat')]
[string]$Value,
# Enter the name of the Environment variable you want modified
[Parameter(Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Default')]
[Parameter(ParameterSetName='Concat')]
[validatescript({$_ -in ((Get-ChildItem -path env:\).name)})]
[string]$Name,
# Enter the type, user or machine
[Parameter(ParameterSetName='Default')]
[Parameter(ParameterSetName='Concat')]
[ValidateSet('Machine','User')]
[string]$Class = 'Machine',
# Enter separator character, defaults to ';'
[Parameter(ParameterSetName='Concat')]
[ValidateLength(0,1)]
[string]$Separator = ';',
# Set to append to current value
[Parameter(ParameterSetName='Concat')]
[switch]$Concatenate
)
begin {}
process {
if ($PSCmdlet.ShouldProcess("$Name","Set Environment Variable to $Value")) {
switch ($Concatenate) {
$true {
$CurrentValue = [Environment]::GetEnvironmentVariable($Name, $Class)
[Environment]::SetEnvironmentVariable($Name, ($CurrentValue + $Separator + $Value) , $Class)
}
$false {
[Environment]::SetEnvironmentVariable($Name, $Value, $Class)
}
}
}
}
end {}
}
#Find full path to bin\git.exe and add it to the PATH variable
Set-EnvVariable -Name PATH -Value (Get-ChildItem -Path $env:LOCALAPPDATA\git* -Filter git.exe -Recurse | Select-Object -First 1 | Select-Object -Property FullName).FullName -Concatenate -WhatIf