Author Posts

July 28, 2015 at 2:34 am

I've been trying to figure something out now for a day and can't find a solution.

So the below code is to create customobject of Application name and date of install.

$application = (Get-WmiObject -Namespace ROOT\cimv2 -class Win32Reg_AddRemovePrograms) | 
ForEach-Object {[PSCustomObject]@{Applications = $_.Displayname; InstallofApplication = $_.installdate}}

I really want to convert the format of the date from 20150511 to dd/MM/yyyy.
Ive tried

 [DateTime]::ParseExact($_.installdate,"yyyyMMdd",$NULL) 

but can't get it to work. Drive me mad !

July 28, 2015 at 3:17 am

Try it like this:

$application = (Get-WmiObject -Namespace ROOT\cimv2 -class Win32Reg_AddRemovePrograms) | 
    ForEach-Object { [PSCustomObject]@{
        Applications = $_.Displayname; 
        InstallofApplication = Get-Date ([datetime]::ParseExact($_.InstallDate,'yyyyMMdd',$null)) -format d}}

July 28, 2015 at 3:18 am

First off – are you sure you have the correct class? Win32Reg_AddRemoveprograms only exists if you have the SCCM client installed.

In terms of date conversion. The WMI cmdlets return dates in WMI's format
£> Get-WmiObject -Class Win32_OperatingSystem | select *date* | fl

InstallDate : 20131205101649.000000+000
LocalDateTime : 20150728121320.002000+060

A method to convert dates is added to all objects returned by Get-WmiObject

£> Get-WmiObject -Class Win32_OperatingSystem | select @{N='Install'; E={$_.ConvertToDateTime($_.Installdate)}}, @{N='Lo
calDate'; E={$_.ConvertToDateTime($_.LocalDateTime)}} | fl

Install : 05/12/2013 10:16:49
LocalDate : 28/07/2015 12:16:26

Alternatively (and easier) use the CIM cmdlets as the date comes pre-converted
£> Get-CimInstance -ClassName Win32_OperatingSystem | select *date* | fl

InstallDate : 05/12/2013 10:16:49
LocalDateTime : 28/07/2015 12:17:29
|

July 28, 2015 at 3:26 am

Hi Richard,

Yes its for an SCCM client.

I will have a play with both your solutions, Many thanks for your quick replies ! 🙂

July 28, 2015 at 4:15 am

Hi Richard,

Doing Get-CIMInstance -ClassName Win32Reg_AddRemoveprograms doesn't return the date in the correct format.

Also, don't think i'm doing something write here,

$application = (Get-WmiObject -Namespace ROOT\cimv2 -class Win32Reg_AddRemovePrograms) | select  @{N='Install'; E={$_.ConvertToDateTime($_.Installdate)}

July 28, 2015 at 6:01 am

See if this works:

Get-WmiObject -Namespace ROOT\cimv2 -class Win32Reg_AddRemovePrograms | select displayname, @{l="Installdate";e={([DateTime]::ParseExact($_.InstallDate,'yyyyMMdd',$null).ToString('MM/dd/yyyy'))}}

July 28, 2015 at 11:16 pm

So by doing this:

$application = Get-WmiObject -Namespace ROOT\cimv2 -class Win32Reg_AddRemovePrograms | 
ForEach-Object { [PSCustomObject]@{
Applications = $_.Displayname;
InstallofApplication = get-date ([datetime]::ParseExact($_.InstallDate,'yyyyMMdd',$null)) -Format d }} 

It generates a error :

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:4 char:24
+ InstallofApplication = get-date ([datetime]::ParseExact($_.InstallDate,'yyyyMMdd ...
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

But strangely the results are correct :

PS C:\WINDOWS\system32> $application

Applications                                                                         InstallofApplication                                                                
------------                                                                         --------------------                                                                
Microsoft Visual C++ 2010  x64 Redistributable - 10.0.40219                          06/07/2015                                                                          
Configuration Manager Client                                                         20/07/2015                                                                          
Microsoft Policy Platform                                                            06/07/2015                                                                          
Microsoft Visual C++ 2013 x64 Additional Runtime - 12.0.21005                        20/07/2015                                                                          
Microsoft Visual C++ 2013 x64 Minimum Runtime - 12.0.21005                           20/07/2015                                                                          
Microsoft Visual C++ 2005 Redistributable (x64)                                      06/07/2015 

Whats going on ? Could there be a hidden header or something ?

July 29, 2015 at 4:11 am

What you'll find is that not all of the applications have the InstallDate property populated. Trying to convert the empty string to a date returns an error.

If you compare the results of

(Get-WmiObject Win32Reg_AddRemovePrograms).count

With

$application.count

after running your script, you'll find the ones without dates are missing from the output.

Methinks some error handling is required.

July 29, 2015 at 4:42 am

Matt, think you've hit the problem !

(Get-WmiObject Win32Reg_AddRemovePrograms).count
= 7

$application.count
= 6

Checking between the two, the additional object is "Intel(R) Graphics Media Accelerator Driver".