Tuesday 11 October 2011

Get-OSInstallDate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets/displays the Sysgtem Uptime after 
  4.     converting it from WEBM time/date format  
  5. .DESCRIPTION 
  6.     This script creates an instance of a SWbemDateTime object,  
  7.     gets the OS install date and converts the date to a more 
  8.     useful format. 
  9. .NOTES 
  10.     File Name  : Get-OSInstallDate.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/aa393687%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh [C:\foo]> .\Get-OsInstallDate.ps1 
  20.     This OS was installed in the year 2008 
  21.     Full installation date (VT_DATE format) is 4/9/2008 8:17:23 PM 
  22.     Full installation date (FILETIME format) is 128522458430000000 
  23.  
  24. #> 
  25.  
  26. # Create swbemdatetime object 
  27. $datetime = New-Object -ComObject WbemScripting.SWbemDateTime 
  28.  
  29. #  Get OS installation time and assign to datetime object 
  30. $os = Get-WmiObject -Class Win32_OperatingSystem 
  31. $dateTime.Value = $os.InstallDate 
  32.  
  33. # Now display the time 
  34. "This OS was installed in the year {0}"           -f $dateTime.Year 
  35. "Full installation date (VT_DATE format) is {0}"  -f $dateTime.GetVarDate() 
  36. "Full installation date (FILETIME format) is {0}" -f $dateTime.GetFileTime()  
  37. ## 

1 comment:

Jeffery Hicks said...

That is a very interesting way of handling the DMTF datetime format. Normally I would have just used the ConvertToDateTime() method on the WMI object and then reformat the datetime object as necessary.

$os=gwmi win32_operatingsystem
$dt=$os.ConverttoDateTime($os.InstallDate)
$dt
$dt.ToFileTime()
$dt.year