Windows 10中SYSTEM账户运行Get-Package为何缺失部分软件包?
SYSTEM账户下Get-Package无法识别用户级软件的原因及替代方案
原因分析
- 安装权限范围差异:Flameshot是用户专属安装的程序,仅对安装它的普通用户可见。SYSTEM作为系统级账户,只能读取系统全局安装的软件包,无法访问普通用户个人账户下的安装记录。
- 注册表存储位置不同:用户级软件的安装信息存放在
HKEY_CURRENT_USER\Software分支,SYSTEM账户运行时访问的是自身的HKCU分支和系统级的HKEY_LOCAL_MACHINE\Software,自然读不到其他用户的安装数据。
替代查询方法(SYSTEM账户可正常运行)
方法1:遍历所有用户注册表提取安装信息
通过加载各用户的NTUSER.dat文件,读取用户级安装的软件记录:
# 获取所有有效用户目录 $userProfiles = Get-ChildItem "C:\Users" -Directory | Where-Object { $_.Name -notin @('Public', 'Default', 'Default User') } foreach ($profile in $userProfiles) { $ntuserPath = Join-Path $profile.FullName "NTUSER.dat" if (Test-Path $ntuserPath) { # 加载用户注册表 hive reg load "HKU\TempUser" $ntuserPath # 查询目标软件(这里以Flameshot为例) Get-ItemProperty "HKU\TempUser\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object { $_.DisplayName -eq "Flameshot" } | Select-Object DisplayName, DisplayVersion, InstallLocation # 卸载注册表 hive,避免占用 reg unload "HKU\TempUser" } }
方法2:合并系统与用户级卸载项查询
直接读取系统全局和所有用户的卸载注册表项,汇总结果:
# 获取系统级安装软件 $systemPackages = Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue # 获取所有用户级安装软件 $userPackages = Get-ChildItem "HKU:\" -Exclude "HKU\.DEFAULT", "HKU\S-1-5-18" | ForEach-Object { Get-ItemProperty "$($_.Name)\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue } # 合并并筛选目标软件 $allPackages = $systemPackages + $userPackages $allPackages | Where-Object { $_.DisplayName -match "Flameshot" } | Select-Object DisplayName, DisplayVersion
方法3:使用CIM查询全量软件信息
借助WMI的CIM接口查询所有注册的软件,包含用户级和系统级:
Get-CimInstance -ClassName Win32_RegistrySoftware | Where-Object { $_.DisplayName -eq "Flameshot" } | Select-Object DisplayName, Version, InstallDate
内容的提问来源于stack exchange,提问作者FHC
相关产品推荐
相关产品推荐

