I've written in the past about converting obtuse WMI datetime formats into more user friendly formats. The other day via Twitter I got a question about the InstallDate property that comes from the Win32_Product class. This property has a different format, than what I've written about previously. And while I think the format is easy to use as is, you may still want to convert it to a friendlier date time object.
First off, here's an example of a Win32_Product object. I've already run a WMI query to return them all and saved the results to a variable, $data.
[cc lang="PowerShell"]
PS S:\> $data[10] | Select Name,Vendor,InstallDate,Version,InstallLocation
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Name : Microsoft Office Office 32-bit Components 2010
Vendor : Microsoft Corporation
InstallDate : 20101117
Version : 14.0.4763.1000
InstallLocation : C:\Program Files\Microsoft Office\
[/cc]
The InstallDate is an 8 character string, that for US based systems is in the format YearMonthDay. I've found you can still use the property in a WMI Query. Suppose I want to find all products installed in the last quarter of 2010.
[cc lang="PowerShell"]
get-wmiobject Win32_Product -filter "installdate <= '20110101' AND installdate >= '20101001'" -AsJob
[/cc]
I find Win32_Product queries very slow so I almost always run them as jobs. But beyond that when I retrieve the results I in fact get only the products installed between Oct 1, 2010 and Dec 31, 2010.
But, if you are preparing reports you may still want a nicer looking format so I threw together an advanced function that will parse the 8 character InstallDate string and return a date time object.
[cc lang="PowerShell"]
Function Convert-InstallDate {
Param(
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage="Enter a 8 character datetime string in the format YYYYMMDD")]
[ValidateLength(8,8)]
[Alias("InstallDate")]
[string[]]$DateString
)
Process
{
Foreach ($string in $datestring)
{
Write-Verbose "Parsing $string"
#parse date values from string
$year=$string.Substring(0,4)
$month=$string.Substring(4,2)
$day=$string.Substring(6)
#build a datetime object
Write-Verbose "$month/$day/$year"
[datetime]$InstallDate="$month/$day/$year"
#write the object to the pipeline
Write-Output $InstallDate
}
}
} #end function
[/cc]
You can either give the function a value or pipe a Win32_Product object to it.
[cc lang="PowerShell"]
PS S:\> $data[10] | Convert-InstallDate
Wednesday, November 17, 2010 12:00:00 AM
[/cc]
With this I can run an expression like this.
[cc lang="PowerShell"]
PS S:\> $data | select Name,@{Name="Installed";Expression={$_ | Convert-InstallDate}} | sort Installed | Group Installed
| Sort Count -descending | Select -first 5 | Format-table Count,Name -autosize
Count Name
----- ----
19 5/5/2010 12:00:00 AM
17 1/20/2010 12:00:00 AM
16 5/3/2010 12:00:00 AM
16 12/1/2010 12:00:00 AM
7 4/13/2010 12:00:00 AM
[/cc]
These are the top 5 busiest installation days for 2010. I'm sure you have plenty of other uses for this function.
Download Convert-InstallDate.
1 thought on “More WMI Dates – Win32Product InstallDate”
Comments are closed.