Powershell – Check and delete files/folders


To totally unlock this section you need to Log-in


Login

Powershell script to delete a file if it already exists:

If (Test-Path $strFileName){

Remove-Item $strFileName
}

Powershell script to check if a file exists:

$strFileName="C:\filename.txt"

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

Powershell script to delete a folder or directory if it exists:

$strFolderName="C:\temp\"

If (Test-Path $strFolderName){
Remove-Item $strFolderName
}