Powershell – Check If File Exists or Not

We can check and test if a file or folder exist or not by using the PowerShell cmdlet Test-Path.

The below powershell script will check whether the file test.txt is already exists or not under the path C:Share.

1
2
3
4
5
6
7
8
9
$FileName = "C:\Sharetest.txt"
if (Test-Path $FileName)
{
  Write-Host "File Exists"
}
else
{
  Write-Host "File Not Exists"
}
 
Advertisement

Leave a Comment