繁体   English   中英

Get-ChildItem 过滤和排序

[英]Get-ChildItem filtered and sorted

我正在尝试 select 注册表中具有 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 我会得到结果,但不会排序。 我哪里错了?

您只需将 pipe 的Get-ChildItem命令输入| Get-ItemProperty | 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

参考

关于潜在的 Specified cast is not valid error with Get-ItemProperty & uninstall registry location

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

$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

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

这是使用 get-itemproperty 的方法:

get-itemproperty hklm:\software\microsoft\windows\currentversion\uninstall\* | 
  where displayname | sort displayname

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM