Q: Reading date and time from a text file using powershell Get-Content I have a text file which contains the last update of a Log file.
 Volume in drive E is ISCSI
 Volume Serial Number is XXXXXX

 Directory of E:\Apps\LOGS

02/26/2020  11:39 AM           762,661-vx_02262020.log
               1 File(s)        762,661 bytes
               0 Dir(s)  12,554,166,272 bytes free

I am using the below powershell script to read the content and email the same:
$Date = (Get-Date -format "MMddyyyy")

$body = Get-Content -path C:\Desktop\Logs\LOG_TIME_$Date.txt -raw

Send-MailMessage -To "abc@gmail.com2" -From "abc@gmail.com"  -Subject "LOG TIME $Date" -Body "$body" -SmtpServer "xxxxx.main.glb.corp.local"

The code works fine and sends all the content of the files in the body to my email. But i only want the date and time (which will keep changing) to be sent in the email.
I have tried lot of options, but none of which seems to work:


*

*used | select-string "05/"

*used Where-Object

*used -Pattern '$Date' -AllMatches |


Can someone help me how to populate only date and time in the email body from the text file  ?

A: To parse out the date and time in the file, you could use this:
$Date = (Get-Date -format "MMddyyyy")

$content = Get-Content -Path "C:\Desktop\Logs\LOG_TIME_$Date.txt" -Raw
if ($content -match '(\d{2}/\d{2}/\d{4}\s+\d{1,2}:\d{2}\s+[AP]M)') {
    $dateString = $matches[1] -replace '\s+', ' '
    $dateInFile = [DateTime]::ParseExact($dateString, 'MM/dd/yyyy h:mm tt', [cultureinfo]'en-UK')
}

$mailParams = @{
    To         = "abc@gmail.com"
    From       = "abc@gmail.com"
    Subject    = "LOG TIME $Date"
    Body       = $dateInFile
    SmtpServer = "xxxxx.main.glb.corp.local"
}

Send-MailMessage @mailParams

Regex details:

(             # Match the regular expression below and capture its match into backreference number 1
   \d         # Match a single digit 0..9
      {2}     # Exactly 2 times
   /          # Match the character "/" literally
   \d         # Match a single digit 0..9
      {2}     # Exactly 2 times
   /          # Match the character "/" literally
   \d         # Match a single digit 0..9
      {4}     # Exactly 4 times
   \s         # Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.)
      +       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   \d         # Match a single digit 0..9
      {1,2}   # Between one and 2 times, as many times as possible, giving back as needed (greedy)
   :          # Match the character ":" literally
   \d         # Match a single digit 0..9
      {2}     # Exactly 2 times
   \s         # Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.)
      +       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   [AP]       # Match a single character present in the list "AP"
   M          # Match the character "M" literally
)



A: Easy done with regex:
$date = $content | ? { $_ -match '(\d{1,2}\/\d{1,2}\/\d{2,4})' } | % { $_ -replace '^(.*)(\d{1,2}\/\d{1,2}\/\d{2,4})(.*)$', '$2' }

