Home

Powershell: Check If File Exists

Posted by SteveHardie | On: Apr 26 2013

Powershell script to check if a file exists

$strFileName="c:\filename.txt"
If (Test-Path $strFileName){
  # // File exists
}Else{
  # // File does not exist
}

See Also:
Powershell: Delete File if Exists
Powershell: Delete Folder if Exists
Test-Path
Remove-Item

5 Comments

  1. Rani Kheir says:

    Works great, thanks!

  2. Nick says:

    Thank you!

  3. 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!”
    }
    else
    {

    }

  4. sagar says:

    Superb… Got my answer in few secs

Leave a comment