4

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

2 Answers 2

11

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
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, Also i how can we remove files which are 10 days older?
2nd answer now there
Is there any way we could log the list of file names into a sql table which will get deleted? Thank you very much for your quick response.
see this page on accessing sql server from powershell - technet.microsoft.com/en-us/library/hh289310.aspx
@prathapkasthuri Please ask one question at a time and preferably show some research effort. These questions already have many answers here on SO.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.