Creating a new registry key by using Windows PowerShell is the same as creating a new file or a new folder. All three processes use the New-Item cmdlet. In addition, you might use the Test-Path cmdlet to determine if the registry key already exists. You may also wish to change your working location to one of the registry drives. If you do this, you might use the Push-Location, Set-Location, and Pop-Location cmdlets. This, of course, is the long way of doing things...
Only the steps:
Store the current working location by using the Push-Location cmdlet.
Change the current working location to the appropriate registry drive by using the Set-Location cmdlet.
Use the Test-Path cmdlet to determine if the registry key already exists.
Use the New-Item cmdlet to create the new registry key.
Use the Pop-Location cmdlet to return to the starting working location.
Assigning a default value to an existing registry key:
Use the Set-Item cmdlet and supply the complete path to the existing registry key.
Supply the default value in the Value parameter of the Set-Item cmdlet.
Use New-Item to create and assign a value
It is not necessary to use the New-Item cmdlet to create a registry key and then to use the Set-Item cmdlet to assign a default value. These steps are combinable to a single command. The following command creates a new registry key with the name hsg1, and it assigns a default value of “default value” to the registry key.
New-Item -Path HKCU:\Software\hsg1 -Value "default value Modifying the value of a registry property value
To modify the value of a registry property value requires using the Set-PropertyItem cmdlet.
Only the steps…
Modifying the value of a registry property value:
Use the Push-Location cmdlet to save the current working location.
Use the Set-Location cmdlet to change to the appropriate registry drive.
Use the Set-ItemProperty cmdlet to assign a new value to the registry property.
Use the Pop-Location cmdlet to return to the original working location.
if((Get-ItemProperty HKCU:\Software\hsg -Name bogus -ea 0).bogus) {'Propertyalready exists'} ELSE { Set-ItemProperty -Path HKCU:\Software\hsg -Name bogus -Value'initial value'} Entering a remote session to create a new registry key
Use the Get-Credential cmdlet to obtain a credential object with rights on the remote computer. Store the returned credential object in a variable.
Use the Enter-PSSession cmdlet to enter a remote Windows PowerShell session on the target computer.
Use the New-Item cmdlet to create the new registry key.
Use the Exit command to leave the remote Windows PowerShell session.
The commands to obtain credentials, enter a Windows PowerShell session, create a new registry key, and leave the Windows PowerShell session are shown here.
$credential = Get-Credential -Credential iammred\administrator
Enter-PSSession -ComputerName sql1 -Credential $credential
New-Item -Path HKCU:\Software -Name HSG -Value "my default value"
Exit Creating remote registry keys on multiple computers
Entering a remote Windows PowerShell session to create a registry key on multiple computers is a tedious and time-consuming process. To perform a command on multiple machines, it is better to use the Invoke-Command cmdlet.
Only the steps…
Using the Invoke-Command cmdlet to create remote registry keys:
Store the server names in a variable.
Store the connection credentials in a variable (use the Get-Credential cmdlet to obtain the credentials).
Use the Invoke-Command cmdlet to run the command against the remote machines. Place the command to be run in the ScriptBlock parameter.
The following commands create a new registry key on the HKCU drive on three different servers.
$servers = "hyperv1","hyperv2","hyperv3"
$credential = Get-Credential -Credential iammred\administrator
Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock {New-Item -Path HKCU:\Software -Name hsg -Value "scripted default value"} Creating property values
It is unusual to have a registry key that has only a default property value. In fact, most registry keys contain multiple property values. To create a new property value, use the New-ItemProperty cmdlet. The following command creates a new property value named NewProperty under the previously created HSG registry key.
New-ItemProperty -Path HKCU:\Software\hsg -Name NewProperty -Value "New PropertyValue" Getting registry property values
When run, the command returns information about the registry key property. In fact, this is the same information that is obtained by using the Get-ItemProperty cmdlet. The use of the Get-ItemProperty cmdlet and the associated output from the command are shown here.
PS C:\> Get-ItemProperty -path HKCU:\Software\hsg -Name newproperty
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\hsg
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software
PSChildName : hsg
PSDrive : HKCU
PSProvider : Microsoft.PowerShell.Core\Registry
NewProperty : New PropertyValue
The newly created registry property appears in the output under the name of the property. This means that to display only the value of the registry property requires essentially naming the property twice. The first time the registry property appears, it is the name of the registry property to obtain; the second time, it is the property to retrieve. This command and the associated output are shown here.
PS C:\> (Get-ItemProperty -path HKCU:\Software\hsg -Name newproperty).newProperty
New PropertyValue