I'm not sure why the registry has been on my mind lately. I probably need a vacation to get out more. But I put together a relatively simple Windows PowerShell function to retrieve registry statistics that you might find useful. My Get-Registry function will return information about the size of a registry as well as its age.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
[cc lang="PowerShell"]
Function Get-Registry {
[cmdletbinding()]
Param (
[Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullorEmpty()]
[String[]]$Computername=$env:Computername
)
Begin {
Write-Verbose "Starting $($myinvocation.mycommand)"
} #Begin
Process {
Foreach ($computer in $computername) {
Write-Verbose "Processing $computer"
Try {
#retrieve registry information via WMI
$data=Get-WmiObject -Class Win32_Registry -ComputerName $computer -ErrorAction Stop
#Format the results and write an object to the pipeline
$data | Select-Object -Property @{Name="Computername";Expression={$_.__SERVER}},
Status,CurrentSize,MaximumSize,@{Name="FreeSize";Expression={$_.MaximumSize - $_.CurrentSize}},
@{Name="PercentFree";Expression={ (1 - ($_.CurrentSize/$_.MaximumSize))*100 }},
@{Name="Created";Expression={$_.ConvertToDateTime($_.InstallDate)}},
@{Name="Age";Expression={(Get-Date) - ( $_.ConvertToDateTime($_.InstallDate)) }}
} #try
Catch {
Write-Warning "Failed to retrieve registry information from $($Computer.ToUpper())"
Write-Warning $_.Exception.Message
}#Catch
}#foreach $computer
} #Process
End {
Write-Verbose "Ending $($myinvocation.mycommand)"
} #End
} #end function
[/cc]
The download version includes comment based help. I kept this pretty simple. The function only takes a computer name as a parameter. You can add support for alternate credentials if you need it. In the Try script block the function invokes Get-WMIObject for the Win32_Registry class.
[cc lang="PowerShell"]
Try {
#retrieve registry information via WMI
$data=Get-WmiObject -Class Win32_Registry -ComputerName $computer -ErrorAction Stop
[/cc]
Remember, when using Try/Catch you must ensure that any exceptions are treated as terminating, which is why I set -ErrorAction to Stop. If there is an error, then the code in the Catch script block executes. Otherwise, the function writes a custom object to the pipeline that includes not only original WMI properties but some calculated properties as well.
[cc lang="PowerShell"]
$data | Select-Object -Property @{Name="Computername";Expression={$_.__SERVER}},
Status,CurrentSize,MaximumSize,@{Name="FreeSize";Expression={$_.MaximumSize - $_.CurrentSize}},
@{Name="PercentFree";Expression={ (1 - ($_.CurrentSize/$_.MaximumSize))*100 }},
@{Name="Created";Expression={$_.ConvertToDateTime($_.InstallDate)}},
@{Name="Age";Expression={(Get-Date) - ( $_.ConvertToDateTime($_.InstallDate)) }}
[/cc]
For example, the Win32_Registry class doesn't have a property to reflect the computername, so I'm using the system property __SERVER and giving it a more user-friendly name. The sizes are in MB. For PercentFree I could have used the -F operator to format it as a percentage. But the result is a string object which wouldn't sort the way you would expect. The function accepts pipelined input or values as an array. Here is a sample of the expected output.
[cc lang="DOS"]
Computername : QUARK
Status : OK
CurrentSize : 67
MaximumSize : 682
FreeSize : 615
PercentFree : 90.1759530791789
Created : 6/12/2010 10:47:40 AM
Age : 325.23:08:08.1087703
[/cc]
If you find this useful I hope you'll let me know (and maybe consider my tip jar).
Download Get-Registry.
Thanks for your advice! Really interesting!