Skip to main content

Examples of creating Registry keys and values in PowerShell.

# Create a New Registry entry, let's create a key.
New-Item -Path HKCU:\SOFTWARE\Techsnips

# Create Registry values
Set-ItemProperty HKCU:\Software\Techsnips -Name Username -Value "Techsnips User" -Type String
Set-ItemProperty HKCU:\Software\Techsnips -Name Version -Value "20" -Type Dword
# We can also use Set-Item... to modify a value
Set-ItemProperty HKCU:\Software\Techsnips -Name Version -Value "30" -Type Dword

# Create an entry to multiple locations, I'll create another key first.
New-Item -Path HKCU:\Software\Test
New-ItemProperty -Path HKCU:\Software\Test, HKCU:\Software\Techsnips -Name Binaries -PropertyType String -Value "c:\techsnips"

# Rename Registry value
Rename-ItemProperty -Path HKCU:\Software\Techsnips -Name Username -NewName ID

# ---------------------------------------------------------------------------------
# Use the PS Provider
# ---------------------------------------------------------------------------------

# Powershell can expose the two main registry keys
Get-PSDrive

Get-PSDrive -PSProvider Registry | Select-Object -Property Name, Root

# Look at Startup items in the Registry
Set-Location -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Get-Item .
Get-ItemProperty .

# Lets use a variable
$mouse = Get-ItemProperty -path 'HKCU:\Control Panel\Mouse'
$mouse.MouseHoverHeight

# Get Registry Permissions
Get-Acl -Path HKCU:\Software
$rights = Get-Acl -Path HKCU:\Software
$rights.Access.IdentityReference

# Test if a Registry Key exists
Test-Path -Path HKCU:\Software\Microsoft

# ---------------------------------------------------------------------------------
# Find and compare registry keys across multiple systems
# ---------------------------------------------------------------------------------

#Region
# Setup Script Variables
$location = "HKLM:\Software"
$key      = "$location\Techsnips"
$creds = Get-Credential

# Retrieve a remote system registry values
$params = @{
    "ComputerName" = "dc1"
    "ScriptBlock"  = {
        Get-ChildItem -Path $Using:key
        Get-ItemProperty -Path $Using:key
    }
}
Invoke-Command @params -Credential $creds
#Endregion

#Region
# Retrieve multiple remote system registry values
$computers = @(
    "DC1"
    "APP1"
)

$computers | Foreach-Object {
    $params = @{
        "ComputerName" = $_
        "ScriptBlock"  = {
            Get-ChildItem -Path $Using:key
            Get-ItemProperty -Path $Using:key
        }
    }
    Invoke-Command @params -Credential $creds
}
#EndRegion

#Region
# Retrieve multiple remote system registry values for comparison
$computers = @(
    "DC1"
    "APP1"
)

$results = $computers | Foreach-Object {
    $params = @{
        "ComputerName" = $_
        "ScriptBlock"  = {
            Get-ItemProperty -Path $Using:key
        }
    }
    Invoke-Command @params -Credential $creds
}
#EndRegion

#Region
$compareResults = $results | Select-Object -Skip 1 | Foreach-Object {
    $excludeProps = @(
        "PSPath"
        "PSParentPath"
        "PSChildName"
        "PSDrive"
        "PSProvider"
        "RunspaceId"
        "PSComputerName"
        "PSShowComputerName"
    )
    $referenceObject = $results[0]
    $referenceProps  = $results[0] | Get-Member | Where-Object { $_.MemberType -EQ 'NoteProperty' -And $excludeProps -NotContains $_.Name } | Select-Object -ExpandProperty Name
} {
    $result = $_
    $referenceProps | Foreach-Object {
        $property = $_
        $compare  = Compare-Object -ReferenceObject $referenceObject -DifferenceObject $result -Property $property -IncludeEqual
        [PSCustomObject]@{
            "ReferenceComputerName"  = $results[0].PSComputerName
            "DifferenceComputerName" = $result.PSComputerName
            "PropertyName"           = $property
            "ReferenceValue"         = $compare | Where-Object { $_.SideIndicator -EQ '==' -Or $_.SideIndicator -EQ '<=' } | Select-Object -ExpandProperty $property
            "DifferenceValue"        = $compare | Where-Object { $_.SideIndicator -EQ '==' -Or $_.SideIndicator -EQ '=>' } | Select-Object -ExpandProperty $property
            "SideIndicator"          = $( If ( $compare.SideIndicator -EQ '==' ) { '==' } Else { '<>' } )
        }
    }
}
$compareResults | Where-Object SideIndicator -NE '==' | Format-Table -AutoSize
#EndRegion