Extracting date from whole line

Welcome Forums General PowerShell Q&A Extracting date from whole line

Viewing 3 reply threads
  • Author
    Posts
    • #164782
      Participant
      Topics: 7
      Replies: 6
      Points: 95
      Rank: Member

      Hello,

      I am trying to extract just date from a whole line of text in description but it’s not working for me. Can someone help. The only output I am getting is 05

       

      Thank You

       

      Output prior to use select-string

      description
      -----------
      Account terminated, disabled on 05/29/2019 16:15:16
      

      Ouput after using select string

      05
      

      code

      $testcsv = import-csv C:\users\username\documents\test.csv
      
      ForEach ($user in $testcsv)
      {
      
      $username = $user.username
      Get-ADUserĀ -FilterĀ {samaccountname -eq $username} -Properties * |
      Select-Object description | Select-String -Pattern "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}" | foreach {$_.Matches.Groups[1].Value}
      
      }
      
    • #164788
      Participant
      Topics: 49
      Replies: 194
      Points: 783
      Helping Hand
      Rank: Major Contributor

      Try this

      $value = "Account terminated, disabled on 05/29/2019 16:15:16"
      # DateTime
      ([regex]::Matches($value, '(\d+/\d+/\d\d\d\d.+)')).Value
      #Date
      (([regex]::Matches($value, '(\d+/\d+/\d\d\d\d.+)').Value).Split(' '))[0]
    • #164800
      Participant
      Topics: 7
      Replies: 6
      Points: 95
      Rank: Member

      Thanks It works but how can I pass output of select-object description to the code that you provide

      $value = “Account terminated, disabled on 5/29/2019 16:16:16”
      ([regex]::Matches($value, ‘(\d+/\d+/\d\d\d\d\d.+)’)).value
      (([regex]::Matches($value, ‘(\d+/\d+/\d\d\d\d.+)’).value).split(‘ ‘))[0]

    • #164815
      Participant
      Topics: 1
      Replies: 64
      Points: 363
      Helping Hand
      Rank: Contributor

      Thanks It works but how can I pass output of select-object description to the code that you provide

      $value = “Account terminated, disabled on 5/29/2019 16:16:16”

      ([regex]::Matches($value, ‘(\d+/\d+/\d\d\d\d\d.+)’)).value

      (([regex]::Matches($value, ‘(\d+/\d+/\d\d\d\d.+)’).value).split(‘ ‘))[0]

       

      Off the top of my head:

      $testcsv = import-csv C:\users\username\documents\test.csv
      
      ForEach ($user in $testcsv){
      write-host $user
      $value=(get-aduser -identity $user -properties *).description
      ([regex]::Matches($value, '(\d+/\d+/\d\d\d\d.+)')).Value
      (([regex]::Matches($value, '(\d+/\d+/\d\d\d\d.+)').Value).Split(' '))[0]
      }
Viewing 3 reply threads
  • The topic ‘Extracting date from whole line’ is closed to new replies.