powershell 数组 - 不拉名称对象

powershell array - not pulling name object

我有脚本可以捕获配置文件列表中的空白配置文件路径。我不确定如何调用数组并删除整个键。它说找不到路径,因为它试图从 c:\windows\system32

运行

我如何让它通过它可能拥有的不同路径?现在我只是通过清空一个配置文件路径来进行测试

$Profiles = get-childitem “HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList”

$ProfileData = @()

foreach($Profile in $Profiles){
$ThisProfileInfo = $null

$ThisProfileInfo = @{Name=$Profile.Name;

                    ProfilePath=$Profile.GetValue("ProfileImagePath")}

   if (!($ThisProfileInfo.ProfilePath)) {
   $ProfileData += $ThisProfileInfo
}

}

$ProfileData

ForEach-Object{Remove-Item -Path ($Object.name)}

错误是 ForEach-Object{Remove-Item -Path ($Object.name)}

Name                           Value                                                                                                                                  
----                           -----                                                                                                                                  
Name                           HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-115761338-1150813220-1225219381-227283            
ProfilePath                                                                                                                                                           
Remove-Item : Cannot find path 'C:\WINDOWS\system32\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows 
NT\CurrentVersion\ProfileList\S-1-5-21-115761338-1150813220-1225219381-227283' because it does not exist.
At line:24 char:16
+ ForEach-Object{Remove-Item -Path ($Object.name)}
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\WINDOWS\syst...25219381-227283:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

最好通过 运行 进行故障排除:

get-childitem “HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList”

当我运行这个时,我得到了两个字段:NameProperty。名称字段只有密钥的名称,但是当我 运行:

get-childitem “HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList” | select Name

我能够获得密钥的完整路径,这可能是之前的方法不起作用的原因。可能只需要添加 | select Name.

tl;dr

以下删除所有代表没有/空 ProfileImagePath 值的用户配置文件的注册表项:

# Note: Run with elevation, otherwise Remove-Item will fail.
Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" |
  Where-Object { -not $_.GetValue('ProfileImagePath') } |
    Remove-Item -WhatIf

注意:上面命令中的-WhatIf common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf


您的主要问题是代表注册表项的 .Name 属性 项是一个 registry-native 键路径(例如 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-18),PowerShell 的 Remove-Item 不识别 [1] 并将 解释为相对 file-system 路径 .

在撰写本文时,您的问题中还有一个附带的语法问题:您的最后两个语句应该是单个管道,如下所示:
$ProfileData | ForEach-Object{ Remove-Item -Path $_.Name }

Get-ChildItem 输出直接传送到 Remove-Item 避免了这个问题,因为它将 .PSPath 属性 值绑定到 Remove-Item-LiteralPath 参数,并且 .PSPath 包含 provider 限定的 路径,即它有一个前缀,将路径标识为 registry路径;例如Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-18

有关 .PSPath 属性 值的来源以及它们如何绑定到 PowerShell 提供程序 cmdlet 的
-LiteralPath 参数的详细信息,请参阅 .


[1] 有一个例外:如果当前位置(如 $PWD / Get-Location 所示) 恰好是目标注册表配置单元(这很不寻常,因为 注册表 位置很少成为当前位置),registry-native 路径 认可。例如,如果您使用 Set-Location HKLM:\
更改为 HKEY_LOCAL_MACHINE 配置单元的根目录 Get-ChildItem HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList 会起作用(有或没有 HKEY_LOCAL_MACHINE\ 前缀)。