Creating Registry Key

I had a question left on the blog asking how to create a registry key. My preferred method is to use the CIM class = StdRegProv. Its a static class so you don’t need to create an object

[uint32]$hklm = 2147483650
$newkey = ‘SOFTWARE\NewKey’

Invoke-CimMethod -ClassName StdRegProv -MethodName CreateKey -Arguments @{hDefKey = $hklm; sSubKeyName = $newkey}

Define the variables for the HIVE in this case HKLM – local machine .  Notice that the value has to be an unsigned integer.

The key you want to create is just the path to the key. if you need to create multiple levels of subkeys they will all create from a single path.

Then use Invoke-CimMethod to call the CreateKey method on StdRegProv. The hash table in Arguments parameter has the method parameter names and appropriate  values.

If everything works you’ll get a return value of 0.

How did I know which parameters the method took?

I used Get-CimClass but that’s a story for another post.

This entry was posted in PowerShell and CIM, PowerShell and WMI. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s