Trying to check folder for a file with todays date

John Dole 21 Reputation points
2022-03-04T20:37:10.223+00:00

Appreciate the help - I am doing something wrong. Trying to look for file in folder with todays date and if it doesnt exist send an email. Every result i have is false. I know its true based on echo but still comes back false. Can someone tell me what I am doing wrong please. As you can see from echo below - I have two 20220304 dates, so it should fire the IF result.

Thanks
John

$today = (Get-Date).ToString('yyyyMMdd')
$creationdates = Get-ChildItem -Path 'D:\Folder' -Force | Select-Object -Property @{Name='LastWriteTime'; Expression={$_.LastWriteTime.ToString('yyyyMMdd')}}

echo $creationdates
echo break
echo $today

if ($creationdates -eq $today)
{
Send-MailMessage -SmtpServer mail.server.local -Subject "Backup Success" -From noreply@ublicly visible .com -To me@keyman .com
}
else
{
Send-MailMessage -SmtpServer mail.server.local -Subject "Backup Failed" -From msweb-oma_noreply@ublicly visible .com -To me@keyman .com
}

echo results

-------------

LastWriteTime

-------------

20220304
20220224
20220225
20220226
20220227
20220228
20220301
20220302
20220303
break
20220304

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points
    2022-03-05T20:10:03.047+00:00

    Using string representation of dates is hardly ever a good idea.

    Using the example @Andreas Baumgarten posted, here's a safer way:

    $today = (Get-Date).Date  
    $Script:Success = $false  
    Get-ChildItem -Path .\Junk -File |   
        ForEach-Object {  
            if ($_.LastWriteTime.Date -eq $today) {  
                $Script:Success = $true  
            }  
        }  
    if ($Script:Success){  
        Send-MailMessage -SmtpServer mail.server.local -Subject "Backup Success" -From noreply@noreply.com -To me@domain.com  
    }  
    else{  
        Send-MailMessage -SmtpServer mail.server.local -Subject "Backup Failed" -From msweb-oma_noreply@noreply.com -To me@domain.com  
    }  
    

    Edit: added code to send an email tailored to the success or failure to find a file written "today".

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 98,626 Reputation points MVP
    2022-03-05T06:59:22.013+00:00

    Hi @John Dole ,

    please try this:

    $today = (Get-Date).ToString('yyyyMMdd')  
    Get-ChildItem -Path .\Junk -File | ForEach-Object {  
      if ($_.LastWriteTime.ToString('yyyyMMdd') -eq $today) {  
        Write-Output "File $($_.Name) has been modified on $($_.LastWriteTime)"  
      }  
      else {  
        Write-Output "$($_.Name) has been modified before today"  
      }  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments