WMI Date Formats … PowerShell to the Rescue
If you use WMI in your PowerShell scripts you’ll probably have noticed that dates are shown in a rather odd format. For example:
Get-WmiObject –Class Win32_BIOS |
Format-Table Description, ReleaseDate -AutoSizeDescription ReleaseDate
----------- -----------
Phoenix ROM BIOS A14 20090511000000.000000+000
While you can read the dates (they are in “year-month-day-hour-minute-second” format), they are difficult to parse quickly, especially if you have a long table filled with them.
Fortunately, PowerShell helps you out by adding the ConvertToDateTime() method to each WMI object. This allows you to do things like this:
$os = Get-WmiObject –Class Win32_OperatingSystem
$os.ConvertToDateTime($os.InstallDate)10 May 2011 14:54:26
It is easy to include this in a table using a calculated property:
$Lease = @{Label=“Lease Expires”;
Expression={$_.ConvertToDateTime($_.DHCPLeaseExpires)}}Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
-Filter "NOT DHCPLeaseExpires = NULL" |
Format-Table Description, $Lease –AutosizeDescription Lease Expires
----------- ------------
Intel(R) WiFi Link 5300 AGN 04/08/2011 18:49:09
Strictly speaking the hashtable could be defined in-line with the Format-Table cmdlet, but I’ve put it on its own line for readability.