prathap kasthuri
prathap kasthuri

Reputation: 53

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.

Pratap

Upvotes: 4

Views: 16862

Answers (2)

Cloud Cho
Cloud Cho

Reputation: 1774

How about Get-ChildItem * -Include *Defrag* -Recurse | Remove-Item from Delete File in Subfolders Recursively by Microsoft?

Upvotes: 0

Ctznkane525
Ctznkane525

Reputation: 7465

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

Upvotes: 11

Related Questions