Thursday, 12 March 2015

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:\Share\test.txt"
if (Test-Path $FileName) 
  Remove-Item $FileName


1 comment: