Q:

Find the most recent file in a folder/dir and send that file in a email using shell script in windows cmd or powershell

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:
EDIT 1:
So far i have this:
This part gives me the last file created in a dir/folder:
$dir = "D:\Users\myUser\Desktop\dirTest"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.Fullname

$attachment = $latest.Fullname

And this send the email  (i'm using yahoo accounts):
$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 )  <---- 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)

A:

With PowerShell, something like this should do alright:
function Send-RecentFile {
    param(
        [ValidateScript({Test-Path $_ })]
        [String] $Path
        )

    $file = Get-ChildItem -Path $Path -File | Sort CreationTime | Select -Last 1
    Write-Output "The most recently created file is $($file.Name)"

    $messageParameters = @{
        From        = "myaccount@mydomain.com"
        To          = "destinationAccount@theirdomain.com"
        Subject     = "title"
        Body        = "message body"
        SMTPServer  = "mail.mydomain.com"
        Attachments = $file.FullName
    }

    Send-MailMessage @messageParameters -Credential (Get-Credential "peopleo@anotherDomain.com")
}

If you do want to store the credentials in the file, you might have to do something slightly different with it.

