Remove specific files in a folder using powershell

I would like to remove specific text files from my folder (D:\Test) using powershell script. Below are the list of files i have in my folder and need to remove files with name "Defrag" in the file name.

  1. Test.txt
  2. Log.txt
  3. Defrag_20180111.txt
  4. Defrag_20180110.txt

I need to remove the files with name "Defrag".

Thanks for your help in advance.


1 answer

  • answered 2018-01-11 20:03 Ctznkane525

    PowerShell is really powerful. This one can be done with one line:

    Remove-Item -Path "pathtoyourfile\*Defrag*.txt"

    And to your second question:

    Get-ChildItem -Path "pathtoyourfile\*Defrag*.txt" -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt (Get-Date).AddDays(-15) } | Remove-Item -Force