Also known as WMI Time, or WBEM DateTime, CIM_DATETime, https://msdn.microsoft.com/en-us/library/aa387237(v=vs.85).aspx, is that odd Windows DateTime format that shows values looking like “20160905103517.816236-240” The COM Object that presents this is WbemScripting.SWbemDateTime, and you frequently see code to convert to this format using it, or a tortuous series of string manipulations. My rule of thumb is this: If you can avoid using a COM object, you should. Here are two functions to handle these dates using .NET.
Function ConvertTo-CIMDateTime{ [OutputType([string])] Param ( # oDT a dateTime object [Parameter(Mandatory=$true, Position=0)] $oDT, # NoUTC is switch to remove UTC data, replacing with 000 [Parameter(Mandatory=$False, Position=1)] [switch]$NoUTC ) $CIMDT = [management.managementdatetimeconverter]::ToDmtfDateTime($oDT) if ($NoUTC){$CIMDT.SubString(0,22)+'000' }ELSE { $CIMDT } }
The second function converts from the CIM DateTime string back to an ordinary date object:
Function ConvertFrom-CIMDateTime{ [OutputType([datetime])] Param ( # strDMTF is string in CIM_DateTime format [Parameter( Mandatory=$true, Position=0)] $strDMTF ) [management.managementdatetimeconverter]::todatetime($strDMTF) }
An example of the output:
#Example $now = Get-date $now $CIMDate= ConvertTo-CIMDateTime $now $CIMDate ConvertFrom-CIMDateTime $CIMDate Monday, September 5, 2016 10:35:17 AM 20160905103517.816236-240 Monday, September 5, 2016 10:35:17 AM