Live Chat
Home » Blog » how to » Delete Only Files Not Folders Using PowerShell – Complete Steps
how to

Delete Only Files Not Folders Using PowerShell – Complete Steps

  author
Published By Ashwani Tiwari
Tej Pratap Shukla
Approved By Tej Pratap Shukla
Published On April 7th, 2025
Reading Time 3 Minutes Reading

Overview: Here, in this blog we are going to know why we need to delete only files and not folders using PowerShell mode. But, before doing this, you should have knowledge about PowerShell and their commands to do it safely.

PowerShell is a popular command line tool that is primarily built for Windows users to automate their tasks and make system processes simple by applying these commands. Often one would use this to clean up a directory by deleting only files while leaving the folders intact. Thus, by doing this, you can maintain the folder structure and eliminate the risk of unknown files.

This approach is especially useful when you’re learning how to remove all files on hard drive? without disturbing the folder hierarchy.

Why Delete Only Files & Keep Folders Safe

There are various possible scenarios in which you just want to delete only files and not folders using PowerShell. Let’s know them in below part:

  • It is suitable for the developer and IT admin users who might want to use it later for reference or automation purposes.
  • On the one hand, you could get rid of temporary or outdated files but keep folder organization safe.
  • You may want to reuse the folder hierarchy for another project while not taking along any old data.
  • Deleting folders in software directories might affect configuration or linked paths.

Delete Only Files and Not Folders Using PowerShell Methods

In this section, you will get four methods to delete only files and save your folders through PowerShell commands.

#1. Delete All Files in a Folder (Keep Subfolders)

Get-ChildItem -Path “C:\Your\Target\Path” -File | Remove-Item

This command will only delete files that are directly inside the folder and not the subfolders of the folder.

#2. Delete All Files Including Subfolder Files

Get-ChildItem -Path “C:\Your\Target\Path” -File -Recurse | Remove-Item

It applies the parameter -Recurse for the removal of all folder files and its subfolders while keeping the folders themselves.

#3. Delete Files Based on Extension

Get-ChildItem -Path “C:\Your\Target\Path” -Recurse -Include *.log -File | Remove-Item

If one wishes to delete certain types of files, such as .log files, by keeping the rest of the data and folders, one can opt for this.

#4. Delete Files Older Than 30 Days

Get-ChildItem -Path “C:\Your\Target\Path” -File -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item

This script removes files older than 30 days while keeping the entire folder structure untouched.

Delete Only Files and Not Folders – Code Free Solution

If you are facing trouble while using these PowerShell commands. So, you can use PCVITA File Eraser Software which gives you the option to select and delete files and not affect folders using simple steps. As, there is no requirement of any technical expertise as it is a code-free solution to do it. By following this tool, you can easily manage your file & folder structure very well for an easy process.

Conclusion 

We have mentioned various steps to delete only files not folders using PowerShell methods. But, it can be complicated as it involves technical steps. So, it is not possible for all the users to perform this task. For that, we have provided you an alternative option which is our automated solution. Follow these steps to delete only files and not folders effectively.

  author

By Ashwani Tiwari

I am an Expert Technical Analyst, specialized in assisting users with complex technological challenges. Through my blogs and articles, I offer expert guidance to help tackle these technical issues effectively. My true passion lies in providing valuable insights and simplifying complicated technicalities, enhancing user understanding and confidence.