Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Registry Size and Age

Posted on May 4, 2011

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Get Registry Size and Age”

  1. Pingback: Get Registry Size and Age - The Lonely Administrator - Powershell.com – Powershell Scripts, Tips and Resources
  2. Erik says:
    May 10, 2011 at 2:22 pm

    Thanks for your advice! Really interesting!

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d