powershell check file date and send email

Tulik23 K 66 Reputation points
2022-02-18T19:33:19.157+00:00

HI
i would like to find a PS that check files on a folder and if there is a new file (from today) i will get email with the name of the file

this is what i had found for now

$folder ="c:\test"

$FolderToday = "D:\test2\today"
$FolderNew = "D:\test2\new"
$FolderOld = "D:\test2\old"
$TodaysFiles = @()
$NewFiles = @()
$OldFiles = @()
$folderFiles = Get-ChildItem -Path $folder -Recurse -File | select Name,CreationTime #| Where-Object {$.Name }#-in $files}
$folderFiles | ForEach-Object {
if($
.CreationTime.Date -eq [datetime]::Today)
{
$tk=Write-Output $folderFiles | select name,CreationTime
Write-Host -ForegroundColor Yellow "today"
$name= Write-Output $folderFiles
#send-MailMessage -From 'files@Mohammad aBu Dayeh .com' -To 'tk@tharun .com' -Subject "'new file arrived today ' " -Body $tk

                                 }  

 
 else  
        {  
            $OldFiles += $_  
            Write-Host -ForegroundColor White "not today"  
        }  

}

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,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 107.5K Reputation points MVP
    2022-02-19T08:23:34.507+00:00

    Hi @Tulik23 K ,

    maybe this helps:

    $today = (Get-Date).ToString('yyyyMMdd')  
    $allFiles = Get-ChildItem -Path .\Junk  
    foreach ($file in $allFiles) {  
      if ($file.CreationTime.ToString('yyyyMMdd') -eq $today) {  
        Write-Output "File $($file.Name) has beens created on $($file.CreationTime)"  
      }  
      else {  
        Write-Output "$($file.Name) has been created before today"  
      }  
    }  
    

    Or just a little shorter:

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

    If this doesn't work for you please post your script here to see the details and running the script for testing. Please use the Code Sample - Ctrl-K option in the editor (the icon with 101010 ) to post scripts here. The Q&A editor is screwing up script code in the normal editor window.
    Also if you get an error message the content of the message would be helpful.

    ----------

    (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

3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 107.5K Reputation points MVP
    2022-02-18T19:42:11.787+00:00
    0 comments No comments

  2. Tulik23 K 66 Reputation points
    2022-02-19T06:06:58.733+00:00

    Thanks
    the second one was the close to what i need,
    i tried to modify it so that i will be able to see the name of the file also not only the date
    ( iknow its a small modification , but cant figure it :) )
    $creationdates = Get-ChildItem -Path c:\test |Select-Object -ExpandProperty CreationTime
    here it takes only the CreatinTime and when i tried to add
    "$creationdates = Get-ChildItem -Path c:\test|select Name,CreationTime #|Select-Object -ExpandProperty CreationTime"
    it failed

    thanks

    0 comments No comments

  3. Tulik23 K 66 Reputation points
    2022-02-19T18:33:57+00:00

    thanks all

    this is what i did finnaly

    $file = "c:\test"

    if($file = Get-Item $file -ErrorAction 0){
    if($file.CreationTime.Date -ne [datetime]::Today)
    {
    Write-Host 'File is not from today'

    Send-MailMessage -SmtpServer <String> -Subject <String> -From <String> -To <String>

    send-MailMessage -From 'files[@](/users/na/?userId=8b217f85-7ffe-0006-0000-000000000000).com' -To 'tk[@](/users/na/?userId=8b217f85-7ffe-0006-0000-000000000000).com' -Subject "'New File Arrived '" -Body $cd  
    }  
    

    else
    {
    Write-Host 'File is from today'
    }

    }


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.