远程删除注册表项

时间:2019-09-20 11:45:11

标签: powershell

我正在尝试删除远程PC上的注册表项,但是看来我无法正常工作。 这是我获得PC名称的方法:

$inputPC = Read-Host 'Enter pc name: '

这是我尝试使用的代码行:

Invoke-Command -ComputerName $inputPC -ScriptBlock {
    Remove-ItemProperty-Path 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run'
}

我还尝试了HKEY_LOCAL_MACHINEHKLM:

,而不是HKLM

我得到了错误:

Cannot find path 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run'
because it does not exist.
    + CategoryInfo          : ObjectNotFound: (HKEY_LOCAL_MACH...rentVersion\Run:String) [Remove-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand
    + PSComputerName        : clt64792

我知道它找不到正确的路径,因为它试图在文件夹中删除它?然后我该如何以我想要的方式访问注册表项?

2 个答案:

答案 0 :(得分:1)

对于这种情况,我建议使用remote registry API

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $inputPC)
$reg.DeleteSubKeyTree('Software\Microsoft\Windows\CurrentVersion\Run')

请注意,必须运行“ RemoteRegistry”服务才能使其正常工作。

答案 1 :(得分:0)

请使用testng.xml代替Remove-Item。希望这行得通

Remove-ItemProperty