简体   繁体   中英

Powershell gets stuck creating a registry key

I tried to create a new registry key using powershell

PS HKCU:\> New-Item -Path .\Software\Google\Chrome\NativeMessagingHosts\ -Name TEST -Value TEST

But somehow the process doesn't return I have to manually exit with CTRL+C When I browse the key with regedit I see it was created, but why does the console not finish the command? Did anyone ever have a similar problem?

As commented, Your code does not work because you need to either provide a proper registry path like

HKCU:\Software\Google\Chrome\NativeMessagingHosts

OR use Set-Location HKCU: first, so the New-Item cmdlet will know where to create something new.

A registry Key does not have a value, only registry entries (properties) do. So in order to create a new key with name "TEST", and create a new entry in that new key, use two lines of code:

New-Item -Path 'HKCU:\Software\Google\Chrome\NativeMessagingHosts' -Name 'TEST'
New-ItemProperty -Path 'HKCU:\Software\Google\Chrome\NativeMessagingHosts\TEST' -Name 'Test' -Value 'Just Testing'

You can also do this in one single statement using .NET SetValue()

Sets the name/value pair on the specified registry key, using the specified registry data type. If the specified key does not exist, it is created.

[Microsoft.Win32.Registry]::SetValue("HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\TEST", "Test", "Just Testing", 1)

The last parameter (in this case 1 ) is the RegistryValueKind you want to set. (in this example a String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM