Get-ChildItem过滤并排序

问题描述 投票:0回答:3

我正在尝试选择注册表中具有DisplayName属性的所有卸载项,并按Displayname排序。我本以为这样会行得通。

$uninstall32 = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstall64 = 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallKeys = (Get-ChildItem "Registry::$uninstall32" | Where-Object {$_.DisplayName} | sort DisplayName) +
                 (Get-ChildItem "Registry::$uninstall64" | Where-Object {$_.DisplayName} | sort DisplayName)

foreach ($uninstallKey in $uninstallKeys) {
    $uninstallKey
}

但是那什么也没返回。如果删除Where-Object,我会得到结果,但未排序。我要去哪里错了?

powershell sorting filtering get-childitem
3个回答
1
投票

您可以将Get-ChildItem命令通过管道传送到| Get-ItemProperty中以获得所需的结果。

话虽这么说,我在注册表中遇到了无效密钥的问题。为了规避可能出现的问题,我对每个项目进行了迭代,并分别获得了该物业。

$uninstall32 = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstall64 = 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallKeys = (Get-ChildItem "Registry::$uninstall32") +
(Get-ChildItem "Registry::$uninstall64")

$AllKeys = `
  Foreach ($key in $uninstallKeys) {
  try {
    $Value = $Key | Get-ItemProperty -ErrorAction Stop
    $Value
  }
  catch {
    Write-Warning $_
  }
}
$AllKeys  = $AllKeys  | WHere DisplayName -ne '' | sort displayname

参考

Regarding the potential Specified cast is not valid error with Get-ItemProperty & uninstall registry location


0
投票

如果我正确理解了这个问题,您要么希望键PATHS作为字符串数组返回,要么希望键作为Objects,包括所有属性:

$uninstall = 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
             'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'

$uninstallKeys = $uninstall | ForEach-Object {
    Get-ItemProperty "Registry::$_" | 
    Where-Object {$_.DisplayName} | 
    Sort-Object DisplayName | 
    # if you want the Keys Paths returned as string properties:
    Select-Object @{Name = 'RegistryKey'; Expression = {($_.PSPath -split '::')[1]}}

    # if you want the Keys returned with all properties as objects:
    # Select-Object $_.PSPath
}

$uninstallKeys

0
投票

get-childitem的输出是一种幻想。它实际上在格式文件中调用get-itemproperty。您需要使用get-itemproperty来查看值和数据。您可能要改用get-package命令。请注意,Netbeans在安装时会生成无效的注册表双字条目“ NoModify”,这会在get-itemproperty中创建异常。

这是一种具有get-itemproperty的方法:

get-itemproperty hklm:\software\microsoft\windows\currentversion\uninstall\* | 
  where displayname | sort displayname
© www.soinside.com 2019 - 2024. All rights reserved.