Powershell – Delete File If Exists

We can test and check if a file exist or not by using the PowerShell cmdlet Test-Path and we can remove/delete a file by using the cmdlet Remove-Item.

The below powershell script delete the file test.txt if it already exists under the path C:Share.

$FileName = "C:\Sharetest.txt"
if (Test-Path $FileName) 
{
  Remove-Item $FileName
}

Advertisement