title: Find the most recent file in a folder/dir and send that file in a email using shell script in windows cmd or powershell
body: <p>I need to find the most recent file in a folder/dir and send that file as an attachment in a email, so far i have this code that find the most recent file in my windows SO, but i need to specify a route a find the most recent file there and then send that file in a email so far i have this:</p>
  
  <p><strong><em>EDIT 1</em></strong>:</p>
  
  <p>So far i have this:</p>
  
  <p>This part gives me the last file created in a dir/folder:</p>
  
  <pre><code>$dir = "D:\Users\myUser\Desktop\dirTest"
  $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
  $latest.Fullname
  
  $attachment = $latest.Fullname
  </code></pre>
  
  <p>And this send the email  (i'm using yahoo accounts):</p>
  
  <pre><code>$emailSmtpServer = "smtp.mail.yahoo.com"
  $emailSmtpServerPort = "587"
  $emailSmtpUser = "yahooAccountThatSendsTheEmail@yahoo.com"
  $emailSmtpPass = "passForThisquestion"
  
  $emailMessage = New-Object System.Net.Mail.MailMessage
  $emailMessage.From = "yahooAccountThatSendsTheEmail@yahoo.com"
  $emailMessage.To.Add( "yahooAccountThatRECIEVESTheEmail@yahoo.com" )
  $emailMessage.Subject = "Testing e-mail"
  $emailMessage.Body = "email from power shell"
  
  $emailMessage.Attachments.Add( $attachment )  &lt;---- this part gives me problems
  
  $SMTPClient = New-Object Net.Mail.SmtpClient($emailSmtpServer, $emailSmtpServerPort)
  $SMTPClient.EnableSsl = $true
  $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser, $emailSmtpPass);
  $SMTPClient.Send($emailMessage)
  </code></pre>