none
PowerShell - Deleting multiple files that contain a specific phrase. RRS feed

  • Question

  • Hi all,

    Can anyone show me an example of how to remove multiple files that contain the same phrase.

    I would like to search through each text file in the folder C:\temp\PS  for the following phrase ""Not a valid request while running in standby mode." and if this phrase exists delete the text file(s).

     Any and all help is appreciated. Thank you in advance.

    Gaz

    Friday, January 31, 2014 7:02 PM

Answers

  • This should do the trick for you :

    $filepath="C:\temp\PS"
    $filelist=Get-ChildItem -Path $filepath -Recurse -Include *.txt | Select-String -Pattern "Not a valid request while running in standby mode"
    foreach ($file in $filelist)
    {
        Remove-Item $file.path
    }

    • Marked as answer by gazzamon Friday, January 31, 2014 9:04 PM
    Friday, January 31, 2014 7:15 PM
  • Hi Gaz,

    Give this a try:

    $string = 'Not a valid request while running in standby mode.'
    
    Get-ChildItem C:\Temp\PS *.txt | ForEach {
    
        If ( (Get-Content $_).Contains($string)) { $_ | Remove-Item }
    
    }

    EDIT: And I should always refresh the page first..... Oh well.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    • Edited by Mike Laughlin Friday, January 31, 2014 7:32 PM
    • Marked as answer by gazzamon Friday, January 31, 2014 9:04 PM
    Friday, January 31, 2014 7:31 PM

All replies