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

Vimes picture Vimes · Nov 3, 2014 · Viewed 51.5k times · Source

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.

Answer

arco444 picture arco444 · Nov 3, 2014

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