Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #Requires -version 2.0
  2.  
  3. #First Run issues
  4. # -----------------------------------------------------------------------------
  5. #Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode.
  6. #AllSigned - Only scripts signed by a trusted publisher can be run.
  7. #RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.
  8. #Unrestricted - No restrictions; all Windows PowerShell scripts can be run.
  9. #
  10. #Set-ExecutionPolicy Unrestricted
  11. #Set-ExecutionPolicy “Unrestricted” -Scope CurrentUser -Confirm:$false
  12. #
  13. #$PSVersionTable.PSVersion get current version
  14. # -----------------------------------------------------------------------------
  15.  
  16. #Reference Links
  17. #https://msdn.microsoft.com/en-us/library/aa394394(v=vs.85).aspx
  18. #https://books.google.ca/books?id=DD1jA3RgFEMC&pg=PA216&lpg=PA216&dq=Win32_Registry+currentSize&source=bl&ots=FOBI7NUgEQ&sig=zLw5f5i9VGpeHnie0Z05vuBDLco&hl=en&sa=X&ved=0ahUKEwjmyfrx1d3NAhVowYMKHUVtC2cQ6AEINTAE#v=onepage&q=Win32_Registry%20currentSize&f=false
  19. # size in MB
  20.  
  21. # -----------------------------------------------------------------------------
  22. # Script: get-RegistrySizeAge.ps1
  23. # Version: 1.2017.05.03
  24. # Author: Mark Pahulje
  25. #    http://metadataconsulting.blogspot.com/
  26. # Date: 08-Apr-2017
  27. # Keywords: Registry, WMI, Size, Age
  28. # Comments:
  29. #
  30. # "Those who forget to script are doomed to repeat their work."
  31. #
  32. #  ****************************************************************
  33. #  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  34. #  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  35. #  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  36. #  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  37. #  ****************************************************************
  38. # -----------------------------------------------------------------------------
  39.  
  40. Function get-RegistrySizeAge {
  41.  
  42. <#
  43. .SYNOPSIS
  44. Get registry size, status, utilization and age
  45. .DESCRIPTION
  46. This command uses WMI to retrieve size and status information for the Windows registry. Sizes are provided in MB. This version has no provision for alternate credentials.
  47. .PARAMETER Computername
  48. The name of a computer to query. The default is the local host.
  49. .EXAMPLE
  50. PS C:\> get-RegistrySizeAge
  51.  
  52. Computername : SERENITY
  53. Status       : OK
  54. CurrentSize  : 185
  55. MaximumSize  : 2048
  56. FreeSize     : 1863
  57. PercentFree  : 90.966796875
  58. Created      : 1/28/2011 4:12:23 PM
  59. Age          : 95.17:40:14.1730245
  60.  
  61. Return registry usage information for the local host.
  62. .EXAMPLE
  63. PS C:\> Get-Content Computers.txt | Get-Registry | Export-CSV c:\work\RegistryReport.csv
  64. Retrieve registry usage information for all the computers in the text file, computers.txt. The results
  65. are exported to a CSV file.
  66. .NOTES
  67. NAME        :  get-RegistrySizeAge
  68. VERSION     :  1.2017.05.03
  69. LAST UPDATED:  5/3/2017
  70. AUTHOR      :  Mark Pahulje
  71. .LINK
  72. http://metadataconsulting.blogspot.ca/2016/07/windows-10-registry-size-number-of-keys-number-of-key-value-pairs.html
  73. .LINK
  74. Get-WMIObject
  75. .INPUTS
  76. String
  77. .OUTPUTS
  78. A custom object
  79. #>
  80.  
  81. [cmdletbinding()]
  82.  
  83. Param (
  84. [Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
  85. [ValidateNotNullorEmpty()]
  86. [String[]]$Computername=$env:Computername
  87. )
  88.  
  89. Begin {
  90.     Write-Verbose "Starting $($myinvocation.mycommand)"
  91. } #Begin
  92.  
  93.  
  94.     #@{Name="Computername";Expression={$_.__SERVER}},  
  95. Process {
  96.  
  97. Write-Host "`r`nGet Registry Age and Size running on " (Get-WmiObject -class Win32_OperatingSystem).Caption -BackgroundColor Red
  98.  
  99.     Foreach ($computer in $computername) {
  100.         Write-Verbose "Processing $computer"
  101.         Try {
  102.          #retrieve registry information via WMI
  103.          $data=Get-WmiObject -Class Win32_Registry -ComputerName $computer -ErrorAction Stop
  104.  
  105.          #direct call to .NET library
  106.          [datetime]$InstallDateMngmtDTC = [System.Management.ManagementDateTimeConverter]::ToDateTime(($data).InstallDate)
  107.          
  108.          Add-Member -InputObject $data -MemberType NoteProperty -Name InstallDateMngmtDTC -Value $InstallDateMngmtDTC
  109.          
  110.          #Format the results and write an object to the pipeline
  111.          #All members here https://msdn.microsoft.com/en-us/library/aa394394(v=vs.85).aspx
  112.          
  113.          $data | Select-Object -Property @{Name="Registry Name";Expression={$_.Name}},
  114.          @{Name="Registry Status";Expression={$_.Status}},
  115.          #@{Name="Registry Description";Expression={$_.Description}},
  116.          #@{Name="Registry Caption";Expression={$_.Caption}},
  117.          @{Name="Current Size";Expression={"{0:N0} Mb" -f ($_.CurrentSize)}},
  118.          @{Name="Free Size";Expression={"{0:N0} Mb" -f ($_.MaximumSize - $_.CurrentSize)}},
  119.          @{Name="Maximum Size";Expression={"{0:N0} Mb" -f ($_.MaximumSize)}},
  120.          @{Name="Percent Used";Expression={"{0:N1} %" -f ( $_.CurrentSize/$_.MaximumSize*100 )}},
  121.          @{Name="Percent Free";Expression={"{0:N1} %" -f ( (1 - ($_.CurrentSize/$_.MaximumSize))*100 )}},
  122.          @{Name="Created";Expression={$_.ConvertToDateTime($_.InstallDate)}},
  123.          @{Name="Age";Expression={(Get-Date) - ( $_.ConvertToDateTime($_.InstallDate)) }},
  124.          @{Name="Age Formatted";Expression={"{1:N0} years, {2:N0} months, {3:N0} days & {0:hh} hours, {0:mm} mins, {0:ss} secs, {0:fff}  msecs" -f (  ((Get-Date) - ($_.InstallDateMngmtDTC)), [Math]::Truncate( (((Get-Date) - ($_.InstallDateMngmtDTC)).Days/365.2425) ), [Math]::Truncate( ((((Get-Date) - ($_.InstallDateMngmtDTC)).Days%365.2425)/30.436875)),  ((((Get-Date) - ($_.InstallDateMngmtDTC)).Days%30.436875))   ) }}
  125.          
  126.         } #try
  127.        
  128.         Catch {
  129.             Write-Warning "Failed to retrieve registry information from $($Computer.ToUpper())"
  130.             Write-Warning $_.Exception.Message
  131.         }#Catch
  132.    
  133.     }#foreach $computer
  134. } #Process
  135.  
  136. End {
  137.     Write-Verbose "Ending $($myinvocation.mycommand)"
  138. } #End
  139.  
  140. } #end function
  141.  
  142. get-RegistrySizeAge