You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PowerShell修改挂载Windows镜像中的Env:Path环境变量

向自定义WinPE Boot.wim添加Path环境变量

问题背景

已编写PowerShell脚本制作包含实用工具、自定义.ps1脚本及PowerShell组件的Boot.wim,所有功能正常运行,但无法将X:\Users\Utilities目录加入WinPE的系统Path环境变量。

解决方法

WinPE的系统环境变量存储在注册表的SYSTEM hive中,需挂载该hive并修改Path键值,将目标目录追加到现有Path末尾。

在原脚本的注册表操作环节(修改PowerShell执行策略之后),添加以下代码:

#--- 挂载WinPE的SYSTEM注册表hive以修改环境变量 ---
$SystemRegLoc = "$($WinPETemp)\Mount\Windows\System32\config\SYSTEM"
reg load "HKLM\WimPE_System" "$SystemRegLoc"

#--- 获取当前系统Path值并追加自定义目录 ---
$EnvRegPath = "HKLM:\WimPE_System\ControlSet001\Control\Session Manager\Environment"
$currentPath = (Get-ItemProperty -Path $EnvRegPath -Name Path).Path
$newPath = "$currentPath;X:\Users\Utilities"

#--- 更新Path环境变量 ---
Set-ItemProperty -Path $EnvRegPath -Name Path -Value $newPath -Force

#--- 卸载SYSTEM注册表hive ---
reg unload "HKLM\WimPE_System"

修改后的完整脚本关键片段

将上述代码插入到原脚本的注册表操作区域,示例位置如下:

#--- Mount WinPE Registry to the Local Registry for mods ---
$RegLoc = "$($WinPETemp)\Mount\Windows\System32\config\software"
reg load "HKLM\WimPE" "$RegLoc"

#--- Modify PowerShell Execution Policy in WIM Registry ---
$RegPath = "HKLM:\WimPE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\"
$NIArgs = @{Path  = $RegPath 
            Name  = 'ExecutionPolicy' 
            Value = "RemoteSigned"
            PropertyType = 'String'
            Force = $True}

New-ItemProperty @NIArgs

reg unload "HKLM\WimPE"  #--- UnMount WinPE registry ---

#--- 新增:挂载SYSTEM hive修改Path环境变量 ---
$SystemRegLoc = "$($WinPETemp)\Mount\Windows\System32\config\SYSTEM"
reg load "HKLM\WimPE_System" "$SystemRegLoc"

$EnvRegPath = "HKLM:\WimPE_System\ControlSet001\Control\Session Manager\Environment"
$currentPath = (Get-ItemProperty -Path $EnvRegPath -Name Path).Path
$newPath = "$currentPath;X:\Users\Utilities"

Set-ItemProperty -Path $EnvRegPath -Name Path -Value $newPath -Force

reg unload "HKLM\WimPE_System"

说明

  • WinPE启动后系统盘默认映射为X:,因此目录路径需写为X:\Users\Utilities
  • ControlSet001是WinPE默认使用的控制集,若需确认可查看注册表中HKLM\WimPE_System\Select下的键值
  • 修改完成后必须卸载注册表hive,避免文件占用导致WIM无法正常卸载

内容的提问来源于stack exchange,提问作者RetiredGeek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:10:28