Powershell: Check If File Exists

Posted by SteveHardie | On: Apr 26 2013

Powershell script to check if a file exists

If (Test-Path $strFileName){
  # // File exists
  # // File does not exist

See Also:
Powershell: Delete File if Exists
Powershell: Delete Folder if Exists


  1. Rani Kheir says:

    Works great, thanks!

  2. Joshua says:

    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!”


  3. sagar says:

    Superb… Got my answer in few secs

  4. Steve says:

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

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

  5. avishivani says:


    Wrapped around () when checking if file does not exist:

    if (-not (Test-Path $path)) {


  6. Wickey says:

    Thanks, this worked for me!
    Just a few lines of code makes life easier.

Leave a comment