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}
}
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]
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]
[quote quote=164800]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]
[/quote]
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]
}