Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a registry value and path leading to it in one line using PowerShell?

Creating a registry value, including the path up to it, and not erroring if the path already exists is easy using old-school reg.exe:

reg add HKCU\Software\Policies\Microsoft\Windows\EdgeUI /f /v DisableHelpSticker /t reg_sz /d 1 

That's nice and concise. The shortest way I found to do it in pure PowerShell is two lines, or three if you don't want to repeat the path:

$regPath = 'HKCU:\Software\Policies\Microsoft\Windows\EdgeUI' New-Item $regPath -Force | Out-Null New-ItemProperty $regPath -Name DisableHelpSticker -Value 1 -Force | Out-Null 

Is there an easier way using pure PowerShell? And without adding a utility function.

like image 332
Vimes Avatar asked Nov 03 '14 17:11

Vimes


People also ask

How do I create a registry path?

If you're creating a new registry value, right-click or tap-and-hold on the key it should exist within and choose New, followed by the type of value you want to create. Name the value, press Enter to confirm, and then open the newly created value and set the Value data it should have.

Which PowerShell cmdlet is used to modify a registry key value?

You can also use the New-ItemProperty cmdlet to create the registry entry and its value and then use Set-ItemProperty to change the value. For more information about the HKLM: drive, type Get-Help Get-PSDrive . For more information about how to use PowerShell to manage the registry, type Get-Help Registry .


2 Answers

You can pipe the creation line to the New-ItemProperty line as follows, but be aware that the -Force flag on New-Item will delete any pre-existing contents of the key:

New-Item 'HKCU:\Software\Policies\Microsoft\Windows\EdgeUI' -Force | New-ItemProperty -Name DisableHelpSticker -Value 1 -Force | Out-Null 
like image 62
arco444 Avatar answered Sep 24 '22 07:09

arco444


Another way of simplifying PS registry handling is calling the .Net function SetValue() directly:

[microsoft.win32.registry]::SetValue("HKEY_CURRENT_USER\Software\Test", "Test-String", "Testing") [microsoft.win32.registry]::SetValue("HKEY_CURRENT_USER\Software\Test", "Test-DW", 0xff) 
like image 29
mschomm Avatar answered Sep 23 '22 07:09

mschomm