send a file via email using a condition (Powershell) RRS feed

  • Question

  • Hi Team

    I want to send Pdf file by email to recipients listed on a CSV file following the following condition

    (If User name in csv = file name) send to username.

    Best Reagard


    Wednesday, July 24, 2019 7:53 AM

All replies

  • Theres probably a more efficient method.

    • Import the csv
    • build a string of each user email (in quotes)
    • trim any remaining commas


    Assuming the csv has a header of 'email'

    [array]$emailList=Import-Csv C:\temp\sendto.csv
    foreach ($user in $emailList) 

    $strTo can then be used as a value in "send-mailmessage -To $strTo"

    if you're comparing the username, where would that come from? a seperate list?

    Wednesday, July 24, 2019 8:06 AM
  • Hi ,

    thanks for your reply ,

    User name is coming from a separate list.

    how can we attach the PDF file after?

    best regards,


    Wednesday, July 24, 2019 8:28 AM
  • Ok

    so assuming you have two CSVs and a common field to lookup with

    userlist.csv : containing username

    emaillist.csv : containing username, email address and filename

    [array]$emailList=Import-Csv C:\temp\emaillist.csv
    [array]$userList=Import-Csv C:\temp\userlist.csv
    foreach ($user in $userlist) 
        $file=($emaillist | Where-Object {$_.username -like "$($user.username)"}).filename
        $email=($emaillist | Where-Object {$_.username -like "$($user.username)"}).email
        if (!($email -eq "")) {
            Write-Output "command to email: Send-MailMessage -To $email -Attachments $file"
            #Send-MailMessage -To $email -Attachments $file
        } else {
            Write-Output "No email found for $($user.username)"

    uncomment the send-mailmessage to send the email (you'll need to add other parameters - mail server, from etc

    Wednesday, July 24, 2019 8:44 AM
  • for this my emaillist.csv contains:


    and userlist.csv contains


    Only bob and ian get an email, because jonathan is not on the userlist, and james doesnt have an email address.

    Wednesday, July 24, 2019 8:47 AM
  • Another way to do the same is to find the common data before the array

    [array]$emailList=Import-Csv C:\temp\emaillist.csv
    [array]$userList=Import-Csv C:\temp\userlist.csv
    $common = $emailList | Where-Object {$_.username -in $userList.username}
    foreach ($user in $common) 
        Write-Output "command to email: Send-MailMessage -To $email -Attachments $file"

    As always with powershell, many ways to do the same task. :)

    Wednesday, July 24, 2019 8:54 AM
  • Hi

    I have a partly powershell script that sends an email from a csv file that contains email addresses 

    However, i need to extend this powershell script that will do the following:-

    I have a folder that contains the following files:-


    So what should happen is:-

    a) I need to search the folders for files called *.royal.pdf, *.coral-pdf, *.corporate.pdf 
    b) once these files are found, the files should be sent indvidually to three seperate emails as attachment, as follows:-

    nbs-royal.pdf file should be sent to
    nbs-coral.pdf should be sent to
    nbs-corporate.pdf should be sent to


    Wednesday, July 24, 2019 8:55 AM
  • do you have a mapping of sorts?

    ie emaillist.csv

    email match -royal.pdf -coral.pdf -corporate.pdf

    and i have a folder called c:\temp\pdfstore


    [array]$emailList=Import-Csv C:\temp\emaillist.csv
    [array]$pdfs=Get-ChildItem -Path $folder -Filter *.pdf
    for ($i=0; $i -lt $pdfs.count; $i++)
        $emailaddress=($emaillist | where-object {$pdfs[$i].name -like "*$($_.match)"}).email
        Write-Output "command to email: Send-MailMessage -To $($emailAddress) -Attachments $($pdfs[$i].fullname)"

    output is:

    command to email: Send-MailMessage -To -Attachments C:\temp\pdfstore\abs-coral.pdf
    command to email: Send-MailMessage -To -Attachments C:\temp\pdfstore\nbs-coral.pdf
    command to email: Send-MailMessage -To -Attachments C:\temp\pdfstore\nbs-corporate.pdf
    command to email: Send-MailMessage -To -Attachments C:\temp\pdfstore\nbs-royal.pdf

    Wednesday, July 24, 2019 9:26 AM
  • Hi Jonathan,

    thanks for your reply,

    as you can see below on output i have the recipient (empty):

    command to email: Send-MailMessage -To  -Attachments C:\Users\amir.belgaied\Desktop\pdfstore\Amir.Belgaied.pdf



    Wednesday, July 24, 2019 10:51 AM
  • Hi Amir

    a blank 'To' would indicate that it couldn't find a match for that file

    I've added a line to collect the blanks and output at the end

    [array]$emailList = Import-Csv C:\temp\emaillist.csv
    [string]$folder = "c:\temp\pdfstore"
    [array]$pdfs = Get-ChildItem -Path $folder -Filter *.pdf
    for ($i = 0; $i -lt $pdfs.count; $i++) {
        $emailaddress = ($emaillist | where-object { $pdfs[$i].name -like "*$($_.match)" }).email
        if ($emailaddress -like "") { $errLog += "Cannot find an email match for file: $($pdfs[$i].name)" } else {
            Write-Output "command to email: Send-MailMessage -To $($emailAddress) -Attachments $($pdfs[$i].fullname)"
    Write-Output $errLog
    The matching of files works on the end of the filename, so anything more complex would probably be more suited to a regex type comparison

    Wednesday, July 24, 2019 11:18 AM
  • Hi Jonathan

    i fixed the match of file and it work,

    but it import all email from "emaillist" and it doesn't send  anything 



    Wednesday, July 24, 2019 11:42 AM
  • Hi Amir

    so the command will need to be added, and you'll need to assign an email server

    you'll need some required fields - the mailserver, from address and subject

    [string]$subject="PDF files attached"

    so the send-mailmessage will look like:

        Send-MailMessage -To $($emailaddress) -From $($from) -Attachments $($pdfs[$i].fullname) -Subject $($subject)  -SmtpServer $($mailFQDN)

    Wednesday, July 24, 2019 11:57 AM
  • Hi Jonathan 

    thank you very much for your collaboration :)

    but it doesn't work again :

    1) doesn't send the email

    2) it show like it send to all users on the emaillist.csv and

    " command to email: Send-MailMessage -To amir.belgaied@***.com anis.hani@****.com -From amir.belgaied@***.com -Attachments C:\Users\amir.belgaied\Desktop\pdfstore\Amir.Belgaied.pdf -Subject PDF files attached  -SmtpServer  ********.com "

    3) It doesn't compare the file name and user name on the list i guess



    Wednesday, July 24, 2019 1:14 PM
  • Hi Amir

    where is the second address after -To coming from ? It can be sent to multiple people, but each one should be in quotes

    So a working command would be:

    Send-MailMessage -To "amir.belgaied@***.com","anis.hani@****.com" -From amir.belgaied@***.com -Attachments "C:\Users\amir.belgaied\Desktop\pdfstore\Amir.Belgaied.pdf" -Subject "PDF files attached"  -SmtpServer  ********.com

    [array]$emailList=Import-Csv C:\temp\emaillist.csv [string]$folder="c:\temp\pdfstore" [array]$pdfs=Get-ChildItem -Path $folder -Filter *.pdf [string]$mailFQDN="" [string]$from="" [string]$subject="PDF files attached"

    [string]$errLog="" for ($i=0; $i -lt $pdfs.count; $i++) { $emailaddress=($emaillist | where-object {$pdfs[$i].name -like "*$($_.match)"}).email if ($emailaddress -like "") {$errLog+="Cannot find an email match for file: $($pdfs[$i].name)"} else { Write-Output "Sending email To $($emailAddress) with Attachments $($pdfs[$i].fullname)"} Send-MailMessage -To $($emailaddress) -From $($from) -Attachments $($pdfs[$i].fullname) -Subject $($subject) -SmtpServer $($mailFQDN) } Write-Output $errLog

    Wednesday, July 24, 2019 1:34 PM
  • Hi Jonathan

    it work very good for sending email 

    but it still sending to all users on emaillisting.csv 

    for exemple amir.belgaied.pdf has been sent  for ( all user) on csv list ,

    but relay it should be sent only for amir.belgaied@***.com and another mail on cc 'like human resource for all operations"

    big thanks Jonathan 


    Wednesday, July 24, 2019 1:51 PM
  • Hi Amir

    Its difficult to say guess where its picking these up from without seeing the data.

    all i can go on is the lists i have above, second guessing im afraid.

    Wednesday, July 24, 2019 2:05 PM