Skip to content

Commit 08cc625

Browse files
committed
feat(env): 增强Import-EnvPath功能支持多模式加载PATH
添加对Machine/User/All三种模式的支持,默认合并系统级和用户级PATH
1 parent 6e21fe2 commit 08cc625

1 file changed

Lines changed: 72 additions & 15 deletions

File tree

psutils/modules/env.psm1

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,86 @@ function Install-Dotenv {
112112
function Import-EnvPath {
113113
<#
114114
.SYNOPSIS
115-
重新加载环境变量中的path
116-
Process 进程级,当前会话生效
117-
User 用户级,当前用户生效
118-
Process 系统级,所有用户生效
115+
重新加载环境变量中的PATH
119116
.DESCRIPTION
120-
重新加载环境变量中的path,这样你在对应目录中新增一个exe就可以不用重启终端就能直接在终端运行了。
121-
.NOTES
122-
Information or caveats about the function e.g. 'This function is not supported in Linux'
123-
.LINK
124-
Specify a URI to a help page, this will show when Get-Help -Online is used.
117+
重新加载环境变量中的PATH,支持三种模式:
118+
- Machine: 仅加载系统级PATH
119+
- User: 仅加载用户级PATH
120+
- All: 合并系统级和用户级PATH(默认)
121+
这样你在对应目录中新增一个exe就可以不用重启终端就能直接在终端运行了。
122+
.PARAMETER EnvTarget
123+
指定要重新加载的PATH类型:
124+
- Machine: 仅系统级PATH
125+
- User: 仅用户级PATH
126+
- All: 合并系统级和用户级PATH
125127
.EXAMPLE
126-
Test-MyTestFunction -Verbose
127-
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
128-
128+
Import-EnvPath
129+
重新加载合并的系统级和用户级PATH(默认行为)
130+
.EXAMPLE
131+
Import-EnvPath -EnvTarget User
132+
仅重新加载用户级PATH
133+
.EXAMPLE
134+
Import-EnvPath -EnvTarget Machine
135+
仅重新加载系统级PATH
136+
.NOTES
137+
合并模式下,系统级PATH优先于用户级PATH
129138
#>
130139

131140
[CmdletBinding()]
132141
param (
133-
[ValidateSet('Machine', 'User', 'Process')]
134-
[string]$EnvTarget = 'User'
142+
[ValidateSet('Machine', 'User', 'All', "Process")]
143+
[string]$EnvTarget = 'All'
135144
)
136145

137-
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", $EnvTarget)
146+
switch ($EnvTarget) {
147+
'Machine' {
148+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
149+
Write-Verbose "已重新加载系统级PATH"
150+
}
151+
'User' {
152+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
153+
Write-Verbose "已重新加载用户级PATH"
154+
}
155+
'Process' {
156+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Process)
157+
Write-Verbose "已重新加载进程级PATH"
158+
}
159+
'All' {
160+
# 获取系统级和用户级PATH
161+
$machinePath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
162+
$userPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
163+
164+
# 合并PATH,系统级在前,用户级在后,并去除重复项
165+
$allPaths = @()
166+
167+
# 添加系统级PATH
168+
if ($machinePath) {
169+
$allPaths += $machinePath -split ';' | Where-Object { $_.Trim() -ne '' }
170+
}
171+
172+
# 添加用户级PATH
173+
if ($userPath) {
174+
$allPaths += $userPath -split ';' | Where-Object { $_.Trim() -ne '' }
175+
}
176+
177+
# 去除重复项,保持顺序(系统级优先)
178+
$uniquePaths = @()
179+
$seenPaths = @{}
180+
181+
foreach ($path in $allPaths) {
182+
$normalizedPath = $path.Trim().TrimEnd('\').ToLower()
183+
if (-not $seenPaths.ContainsKey($normalizedPath) -and $normalizedPath -ne '') {
184+
$uniquePaths += $path.Trim()
185+
$seenPaths[$normalizedPath] = $true
186+
}
187+
}
188+
189+
$env:Path = $uniquePaths -join ';'
190+
Write-Verbose "已重新加载合并的系统级和用户级PATH,共 $($uniquePaths.Count) 个唯一路径"
191+
}
192+
}
193+
194+
Write-Host "PATH已重新加载" -ForegroundColor Green
138195
}
139196

140197

0 commit comments

Comments
 (0)