命令中的数组迭代以查找子项

时间:2019-07-11 18:54:47

标签: windows powershell

我创建了一个文本文件,其中包含父文件夹中的子项。我删除了标题并调用了文件。我可以遍历文件,但是无法在新的父目录中保留项目。

#create txt for array
Get-Item -Path HKLM:\test\Software\Microsoft\Windows\Shell\Bags\* | Out-File C:\test\shell.txt
$files = "C:\test\shell.txt"
#removes header
get-content $files | select -Skip 7 | set-content "$files-temp"
move "$files-temp" $files -Force
#iterates array
Get-Content $files | ForEach-Object { Get-Item -Path HKLM:\test\Software\Microsoft\Windows\Shell\Bags\$_\* }

我需要能够遍历列表并获取要迭代的文件夹内的信息。

当前输出示例:

Hive: HKEY_LOCAL_MACHINE\TEST\Software\Microsoft\Windows\Shell\Bags


Name                           Property                                                                                                                
----                           --------                                                                                                                
1                                                                                                                                                      
10                                                                                                                                                     
11                                                                                                                                                     
12                                                                                                                                                     
13                                                                                                                                                     
14  

_____________________________________________________________________________ 解决方案

$filedir = "HKLM:\test\Software\Microsoft\Windows\Shell\Bags"
foreach($file in Get-ChildItem -Recurse $filedir){
    echo $file >> "C:\test\shell.csv" 
}

1 个答案:

答案 0 :(得分:1)

实际上,我不太喜欢powershell如何做注册表。这是一个名为“ get-itemproperty2.ps1”的脚本:

param([parameter(ValueFromPipeline)]$key)

process { 
  $valuenames = $key.getvaluenames() 

  if ($valuenames) { 
    $valuenames | foreach {
      $value = $_
      [pscustomobject] @{
        Path = $key -replace 'HKEY_CURRENT_USER',
          'HKCU:' -replace 'HKEY_LOCAL_MACHINE','HKLM:'
        Name = $Value
        Value = $Key.GetValue($Value)
        Type = $Key.GetValueKind($Value)
      }
    }
  } else {
    [pscustomobject] @{
      Path = $key -replace 'HKEY_CURRENT_USER',
        'HKCU:' -replace 'HKEY_LOCAL_MACHINE','HKLM:'
        Name = ''
        Value = ''
        Type = ''
    }
  }
}

完成该操作后,您可以执行以下操作:

get-item HKLM:\Software\Microsoft\Windows\currentversion\run | get-itemproperty2

Path                                                Name           Value                                                  Type
----                                                ----           -----                                                  ----
HKLM:\Software\Microsoft\Windows\currentversion\run SecurityHealth C:\Program Files\Windows Defender\MSASCuiL.exe ExpandString
HKLM:\Software\Microsoft\Windows\currentversion\run DagentUI       C:\Program Files\Altiris\Dagent\dagentui.exe         String
HKLM:\Software\Microsoft\Windows\currentversion\run KeyAccess      kass.exe                                             String

您可以轻松地将其导出到csv。注意,我使用get-item来获取顶级键属性。 get-childitem甚至无法做到这一点。但是您可以将get-childitem -recurse传递给get-itemproperty2。

相关问题