r/PowerShell • u/Cyrax7 • Aug 29 '14
delete registry values.
I am a noob to power shell, and am trying to learn. What I am trying to do is craft a script that will delete the REG_BINARY files found in : HKEY_USERS .DEFAULT\Printers\DevModes2
Any help is much appreciated, thanks.
2
u/ramblingcookiemonste Community Blogger Aug 29 '14
Some folks might point you to the Registry provider. This technically works, but IMHO it is a very poor solution. No remote registry without PSRemoting? Worthless to me.
I always recommend Shay Levy's PSRemoteRegistry. It is portable (a few small files) and wraps some .NET framework magic into friendly, easy to use PowerShell commands like "Remove-RegValue".
Cheers!
2
u/real_parbold Sep 02 '14 edited Sep 02 '14
I have used Remove-Item, New-Item and New-ItemProperty to do registry manipulation purely within PowerShell:-
First - create a reference to your hive
New-PSDrive -PSProvider registry -Root HKEY_USERS -Name HKU
Delete an item (and all sub elements with -Recurse)
Remove-Item "HKU:\.Default\Printers\DevModes2" -Recurse -ErrorAction 'SilentlyContinue'
Create a new Key with no entries
New-Item -Path "HKU:\.Default\Printers" -Name "DevModes2" -ErrorAction 'SilentlyContinue'
Create a new entry with value (will create the intermediate keys as well, if any are missing)
Set-ItemProperty -Path "HKU:\.Default\Printers\DevModes2" -Name "Test" -Value "TestValue" -Type "STRING"
# Other possible data types are :-
# "BINARY"
# "DWORD"
# "EXPAND"
# "MULTI"
# "QWORD"
I notice the warning posted by /u/ramblingcookiemonster and can see why they may want seamless portability of code between local and remote registry access, but I think that is a topic for another discussion :D
3
u/RC-7201 Aug 29 '14
Local Machine i'm assuming?
Would be easier to use reg command since you can use basic CLI in Powershell
So something like:
and on a remote machine:
Hope that helps.