Tautvis
Tautvis

Reputation: 101

Remotely deleting registry keys

I am trying to delete registry keys on a remote PC, but it seems I can't get the path to work correctly. This is how I get the PC name:

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

And this is the code line I try to delete it with:

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

Instead of HKEY_LOCAL_MACHINE I have tried also HKLM: and HKLM

I get the error:

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

I understand it does not find the correct path, because it tries to delete it within the folders? How do I then access the registry keys the way I want to do it?

Upvotes: 0

Views: 7327

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

I'd recommend using the remote registry API for things like this.

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

Note that the service "RemoteRegistry" must be running for this to work.

Upvotes: 1

Suhas Parameshwara
Suhas Parameshwara

Reputation: 1454

Please use Remove-Item instead of Remove-ItemProperty. Hope this works

Invoke-Command -ComputerName $inputPC -ScriptBlock { Remove-Item -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Run}

Upvotes: 0

Related Questions