Script to check if zip file exist and send email


I’m trying to write a script to check for a file in a path and send an email. please see the script and let me know where i’m going wrong.

$file1 = C:\test.txt

#Email Settings
$MailFrom = “”
$Mailto = “”
$MailSubject = “Status of backup file”
#$body = “The following files are missing:`r`n”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#Check for Existence of Files
if (Test-Path $file1)

echo “File $file already exists” ;
Send-MailMessage -to $Mailto -from $MailFrom -subject $MailSubject -body “$file1 exist on the path” -smtpServer

 #Send Email
 Send-MailMessage -to $Mailto  -from $MailFrom  -subject $MailSubject -body "$file1 does not exist on the path" -smtpServer  


i’m getting the following error

est-Path : Cannot bind argument to parameter ‘Path’ because it is null.
At line:12 char:14

  • if (Test-Path <<<< $file1)
    • CategoryInfo : InvalidData: (:slight_smile: [Test-Path], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathComman

Also, my file name changes with date like test1103 ( today ) test1104 ( tomorrow ). can anyone please help me on this ?

Thanks in advance


Try this to fix the Test-Path issue:

$file1 = "C:\test.txt"


You need to place quotation marks (single or double, either will work) when assigning a string value to the $file1 variable:

$file1 = C:\test.txt

# Should be:

$file1 = 'C:\test.txt'

Your issue is the $file1 variable is not recognized as a string. The path should be enclosed in qoutes:

PS C:\Users\rsimmers> $test = C:\Test\123.txt
C:\Test\123.txt : The term 'C:\Test\123.txt' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that 
the path is correct and try again.
At line:1 char:9
+ $test = C:\Test\123.txt
+         ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Test\123.txt:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\rsimmers> $test = "C:\Test\123.txt"

You can shorten this to single line, but I made it two so it’s somewhat easier to read. This would produce an email body like “File Exists: True”:

$file = "whatever{0}.zip" -f (Get-Date).ToString("MMdd") #Custom Date time format:
Send-MailMessage -to -from -subject "File Checker" -body ("File Exists: {0}" -f (Test-Path -Path $file)) -smtpServer

Thanks a lot everyone…

it worked. but my filename changes with with date or time at the end of it… is there any way to find it ?

file name will be same but numbers ( time or date ) changes
e.g., ,

Rob, thanks for the code. Is there any way to find the file using strings or something like this ?

like $filename = “”


Yes, that is what I posted.

$file = “whatever{0}.zip” -f (Get-Date).ToString(“MMdd”)

results in:

PS C:\Users\rsimmers> “whatever{0}.zip” -f (Get-Date).ToString(“MMdd”)

Thanks again Rob…

we don’t know which date it will be so it’s not safe to use mmdd…i’m thinking to find the filename using extension ( because we will be having only one file with .zip extension )

also, in the above reply you mentioned “$file = “whatever{0}.zip” -f (Get-Date).ToString(“MMdd”)” but $file already contains path right ? how to proceed with this ?

You would use Get-ChildItem with a wildcard, something like this:

$file = @(Get-ChildItem "C:\users\rsimmers\desktop\help*.zip" | Select FullName, LastWriteTime)

if ($file.Count -gt 0) {
    $body = "Found {0} zip files: `r`n`r`n{1}" -f $file.Count, ("{0} - {1}" -f $file.FullName, $file.LastWriteTime)
else {
    $body = "No zip files found, rut roh raggy"


Depending on the version of Powershell, you would have to test formatting. If there is a possibility of multiple files, I would use HTML email and use ConvertTo-HTML to generate a table from the $file object.

Thanks a lot Rob for your help…

i’m not getting the full output but it’s finding the .zip file.

btw can you tell me what is – ?

code is some what stuck here

i’m seeing this output :

Found 1 zip files:

Our forums software is still HTML escaping some things in code blocks that are occasionally annoying. I think that particular sequence should just be a hyphen character.