8 thoughts on “Powershell: Check If File Exists”

  1. Hey, thanks you this example saved the day for me! I would like to let everyone know that when using test-path in a if statement with a -Or you will need to put ( ) around each test-path for example:

    $x = C:/test1.txt
    $y = C:/test2.txt

    if ((test-path $I) -or (test-path $y))
    {
    write-host “hello world!”
    }
    else
    {

    }

  2. under PS 5, I found I had to put () around the variable as well:

    if (Test-Path -LiteralPath ($pricelist)) {
    $message.attachments.add($pricelist)
    $msg = $pricelist+’ attached to email’
    Write-Host $msg
    }
    else {
    $msg = $pricelist+’ not found’
    Write-Host $msg
    }

Comments are closed.