Extracting date from whole line

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

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
2 months, 2 weeks ago.

  • Author
    Posts
  • #164782

    Participant
    Topics: 5
    Replies: 5
    Points: 54
    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: 37
    Replies: 155
    Points: 479
    Helping Hand
    Rank: 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: 5
    Replies: 5
    Points: 54
    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: 59
    Points: 328
    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]
    }

You must be logged in to reply to this topic.