Search code examples
powershellparsingoutputpowershell-cmdlet

PowerShell command output convert to date and time


How would one convert a PowerShell command output that retrieves some date/time like to a readable date/time string? The command is

((Get-ComputerRestorePoint)[-1]).CreationTime

Which basically retrieves the last restore point created date and time, but in a weird format like 20190109100614.556883-000


Solution

  • The conversion can be done via RestorePoint object. Like so,

    $rp=((Get-ComputerRestorePoint)[-1])
    $rp.ConvertToDateTime($rp.CreationTime).ToString("u")
    
    # Result
    2019-01-08 08:24:24Z
    

    The u format is universal sortable sepcifier, there are quite a few alternatives too.