Repository navigation
Expand file tree
/
Copy pathadd-path.ps1
More file actions
106 lines (101 loc) · 2.73 KB
/
Copy pathadd-path.ps1
File metadata and controls
106 lines (101 loc) · 2.73 KB
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
##
# @file
#
# @brief Add to PATH environment variable.
#
# @version 1.0
# @author Wei Tang <gauchyler@uestc.edu.cn>
# @date 2021-03-13
#
# @copyright Copyright (c) 2021.
# National Key Laboratory of Science and Technology on Communications,
# University of Electronic Science and Technology of China.
# All rights reserved.
param(
[switch] $sys = $false,
[Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)
# By default, add current directory to `PATH`.
If ($args -eq $null)
{
$args = @("$(Get-Location)")
}
##
# @param[in] $path The string value of `PATH` environment variable.
# @param[in] $coll The array of paths to be added.
function AppendPaths([string] $path, $coll)
{
$paths = $path -split ';'
$result = @()
ForEach($dir In $paths)
{
If ($dir -ne "")
{
$result = $result + $dir
}
}
ForEach($dir In $coll)
{
# Prevent duplication.
If ($result -notcontains $dir)
{
$result = $result + $dir
}
}
Return $result -join ';'
}
function DoWork([switch] $sys = $false)
{
$coll = @()
ForEach($arg In $args)
{
If ([System.IO.Directory]::Exists($arg))
{
$coll = $coll + [System.IO.Path]::GetFullPath($arg)
}
Else
{
Write-Host "The directory '$arg' does not exist."
}
}
If ($coll)
{
If ($sys)
{
$path = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine)
$path = AppendPaths -path $path -coll $coll
Write-Host $path
[System.Environment]::SetEnvironmentVariable('PATH', $path, [System.EnvironmentVariableTarget]::Machine)
}
Else
{
$path = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)
$path = AppendPaths -path $path -coll $coll
Write-Host $path
[System.Environment]::SetEnvironmentVariable('PATH', $path, [System.EnvironmentVariableTarget]::User)
}
}
}
If ($sys)
{
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
DoWork -sys @args
# Write-Host "Press any key ..."; $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
else
{
$argList = "-File $PSCommandPath -sys"
Foreach ($arg In $args)
{
$argList += ' "' + $arg + '"'
}
Start-Process -FilePath "powershell" -Verb RunAs -ArgumentList $argList
}
}
Else
{
DoWork @args
if($psISE){(New-Object -ComObject 'WScript.Shell').Popup('Click OK to continue...',0,'Script done',0)}else{Write-Host 'Done. Press any key to continue...' -NoNewline;$key=$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode}
}