PowerShell – Search and Delete Files (Extension Search)

I wrote this script to find file’s with  specific extension and take action such as delete.

$_SearchExtension=Read-Host "search file (extension *.trn,*.BAK etc ):" 
$_SourcePath=Read-Host "Folder location:" 
$_Action=Read-Host "Action (0=Print,1=Print and Delete):" 
$_fileex=$_SearchExtension Write-Host "" $_Fpath=$_SourcePath
$_files = get-childitem -Path $_Fpath -filter $_fileex -Recurse -ErrorAction SilentlyContinue -Verbose |? {$_.PsIscontainer -eq $false } 
foreach($_f in $_files) 
   if($_Action -eq 0) 
      Write-Host "FileName : " $_f.fullname
    elseif($_Action -eq 1) 
      Write-Host "FileName : " $_f.fullname remove-item $_f.fullname 



Leave a Reply

Your email address will not be published. Required fields are marked *