PSCustomObject convert install date

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Graham Beer Graham Beer 1 year, 10 months ago.

  • Author
    Posts
  • #27939
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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 !

  • #27940
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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}}
    
  • #27941
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    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
    |

  • #27942
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi Richard,

    Yes its for an SCCM client.

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

  • #27944
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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)}
    
  • #27948
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    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'))}}

  • #27976
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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 ?

  • #27979
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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.

  • #27980
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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".

You must be logged in to reply to this topic.