有没有办法检查我安装的程序的范围?我想知道某些程序是否只适用于CurrentUser,或者它们是否可以由AllUsers使用。最好通过PowerShell,但我想它不一定是。
要使用Get-Command
和Where-Object
cmdlet,在位于当前用户主目录树外部目录的PATH(位于$env:PATH
中列出的目录中(中查找所有可执行文件:
# Finds all executables in $env:PATH that are *not* located in the
# current user's home-directory tree.
Get-Command -Type Application |
Where-Object { -not $_.Path.StartsWith($HOME, 'OrdinalIgnoreCase') }
要,请使用计算的属性对给定的可执行文件名提问:
# For the given executable names, outputs [pscustomobject] instances
# containing each executable's full path and a flag that indicates
# whether the executable's directory is located in the
# current user's home-directory tree.
Get-Command -Type Application foo, bar |
Select-Object Path,
@{ Name='CurrentUserOnly'; Expression={ $_.Path.StartsWith($HOME, 'OrdinalIgnoreCase') } }
如果要包含不能从shell直接调用的可执行文件(其目录在$env:PATH
中不是(,但可以通过start
(cmd.exe
(/Start-Process
(PowerShell(/通过GUI的Run
对话框(WinKey-R(调用的可执行程序,例如excel.exe
:
# Finds all executables in $env:PATH *and* those that can be launched
# via cmd /c start / Start-Process, which are *not* located in the
# current user's home-directory tree.
# Lists full paths only.
@(Get-Command -Type Application).Path +
(Get-ChildItem 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionApp Paths').
ForEach({ if ($val = $_.GetValue('') -replace '"') { $val } }) |
Where-Object { -not $_.StartsWith($HOME, 'OrdinalIgnoreCase') }
注意:不幸的是,Get-Package
-IncludeWindowsInstaller
不是选项,因为虽然它确实列出了已安装的应用程序,但它似乎:
在Windows PowerShell中不能与
-Scope CurrentUser
或-Scope AllUsers
组合;此外,至少在默认情况下,PowerShell(Core(7+中根本不支持-IncludeWindowsInstaller
/-ProviderName Programs
。也不会返回有关已安装程序的可执行路径的信息。
我尝试了get包的-scope参数,但这不适用于应用程序。这里有另一种方法(Powershell 5.1(。这些按用户的应用程序往往是管理员的克星。
get-package | ? fastpackagereference -match hklm # 130 items
get-package | ? fastpackagereference -match hkcu | ft -a
Name Version Source ProviderName
---- ------- ------ ------------
Cisco Webex Meetings 41.5.4 Programs
Amazon Kindle 1.31.0.60170 Programs
f.lux Programs
Microsoft OneDrive 21.073.0411.0002 Programs
Slack 4.16.1 Programs
Microsoft Teams 1.4.00.4167 Programs
Microsoft Visual Studio Code (User) 1.55.2 Programs