Remote Registry with Powershell (PLUS: Create-ListBox function)

Until the powers of remoting arrive in Powershell v2, accessing a remote registry is a bit more cumbersome than a local registry. As you no doubt know, Powershell allows you to browse through the local registry as if it was a filesystem. The folling function should help you with accessing a remote registry:

function Enumerate-RemoteRegistryValues
 $ServerName = Read-Host “Server”
 $Hives = `
 $Hive = Create-ListBox -Items $Hives -Title “Select Registry Hive”`
 -Text “Select a remote registry hive to connect to:”
 $baseKeyName = Read-Host “Enter Key Name”
 $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(`
[Microsoft.Win32.RegistryHive]::$Hive, $serverName)
 $baseKey = $registry.OpenSubKey($baseKeyName)
 $ValueNames = $baseKey.GetValueNames()
 $myCol = @()
 ForEach ($ValueName in $ValueNames)
  $myObj = “” | Select Name, Value
  $myObj.Name = $ValueName
  $myObj.Value = $baseKey.GetValue($ValueName)
  $myCol += $myObj
 return $myCol

Oh yeah!
One thing though. The real fanatics should have spotted it by now: The cmdlet Create-ListBox is not a native Powershell cmdlet. If fact, it is a function I created. It allows the user of the script to select a value from a collection of values using a sleek List Box. You should load this function before using the one above. Here’s how I did it:

# Allows you to create a listbox to let a user choose one of several values
function Create-ListBox
param ([array]$Items,[string]$Title = “ListBox”,[string]$Text = “Please make your selection”)
If ($Items -eq $null){Write-Warning ‘Argument “Items” is mandatory.’;throw “Syntax error.”}[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”) | Out-Null
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = $Title
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = “CenterScreen”
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Escape”)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “OK”
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = $Text

$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80

ForEach ($item in $Items)
[void] $objListBox.Items.Add($item)


$objForm.Topmost = $True

[void] $objForm.ShowDialog()

return $x

2 thoughts on “Remote Registry with Powershell (PLUS: Create-ListBox function)

Leave a Reply