Home > Mobile >  Need to convert Win32_PrintJob - TimeSubmitted to a standard Date/Time format using PowerShell
Need to convert Win32_PrintJob - TimeSubmitted to a standard Date/Time format using PowerShell

Time:07-28

gwmi -Query "Select * From Win32_PrintJob Where DriverName = 'Lexmark Universal v2 PS3'" | select Document, Size, TotalPages, TimeSubmitted  | epCsv -Path C:\PrintJobs2.csv -NoTypeInformation

"TimeSubmitted" shows up as: "20220722094242.976000-240"

I've seen other variation of using $_.ConvertToDateTime or DateTime.ParseExact("20130608204517.699000-300".Split('.')[0], "yyyyMMddHHmmss", null); but not sure how to format it correctly with my code above...

tysm

CodePudding user response:

I'm not able to test myself but give this a try

gwmi -Query "Select * From Win32_PrintJob Where DriverName = 'Lexmark Universal v2 PS3'" | select Document, Size, TotalPages, @{n="TimeSubmitted";e={[DateTime]::ParseExact($_.TimeSubmitted.Split('.')[0], "yyyyMMddHHmmss", $null) }}  | epCsv -Path C:\PrintJobs2.csv -NoTypeInformation

CodePudding user response:

That date format is called DMTF datetime and represents a date or time value in a string in DMTF date/time format:

  • yyyymmddHHMMSS.mmmmmmsUUU, where
  • yyyymmdd is the date in year/month/day
  • HHMMSS is the time in hours/minutes/seconds
  • mmmmmm is the number of microseconds in 6 digits
  • sUUU is a sign ( or -) and a 3-digit UTC offset.

This value maps to the CimType DateTime type (101).

Luckily, there is a way to convert that to a normal DateTime object in the current time zone of the system using

[System.Management.ManagementDateTimeConverter]::ToDateTime("20220722094242.976000-240")

Your code can then be

Get-WmiObject -Query "Select * From Win32_PrintJob Where DriverName = 'Lexmark Universal v2 PS3'" | 
Select-Object Document, Size, TotalPages, 
              @{Name = 'TimeSubmitted'; Expression = {[System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeSubmitted)}} -ExcludeProperty TimeSubmitted |
Export-Csv -Path C:\PrintJobs2.csv -NoTypeInformation

Note:

Unfortunately, the ManagementDateTimeConverter returns a datetime with its .Kind set to Unspecified where that shoud really be Local.
In your case this doesn't matter, but if you want to use this datetime object in further calculations, you can force set it to Local using:

$date = [System.Management.ManagementDateTimeConverter]::ToDateTime("20220722094242.976000-240")
[datetime]::SpecifyKind($date, 'Local')
  • Related