Convert string yyyy-MM-dd’T’HH:mm:ss’Z to date

Welcome Forums General PowerShell Q&A Convert string yyyy-MM-dd’T’HH:mm:ss’Z to date

Viewing 2 reply threads
  • Author
    Posts
    • #216348
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Hi,

      I’m  using Get-AzureADAuditSignInLogs cmdlet from the Azure AD Public Preview module to audit last sign in times for users.

      Like this:

      $lastSignIn = get-AzureADAuditSignInLogs | select UserDisplayName,UserPrincipalName,CreatedDateTime

      I’m particular interested in the CreatedDateTime property which is returned as a string in this format:

      ame MemberType Definition
      —- ———- ———-
      CreatedDateTime NoteProperty string CreatedDateTime=2020-04-07T16:55:35.439072Z

      I eventually want to compare it against another date but in it’s current format will not be able to do it. I’m struggling to convert the string to a date format PowerShell recognises.

      Any ideas?

      Thansk

       

       

       

       

    • #216354
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      Parses fine for me:

      PS C:\WINDOWS\system32> [datetime]::Parse('2020-04-07T16:55:35.439072Z')
      
      Tuesday, April 7, 2020 12:55:35 PM
      
      
      PS C:\WINDOWS\system32> Get-Date -Date '2020-04-07T16:55:35.439072Z'
      
      Tuesday, April 7, 2020 12:55:35 PM
      

      Code:

      $lastSignIn = get-AzureADAuditSignInLogs | 
                    select UserDisplayName,
                           UserPrincipalName,
                           @{Name='CreatedDateTime';Expression={[datetime]::Parse($_.CreatedDateTime)}}
      
    • #216378
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      This works. Thanks a lot.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.