- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete empty files and folders using PowerShell?
To delete empty files and folders, we need to first retrieve the list and which has been shown in the earlier articles.
Example
In this article, we are using the logic that if we find an empty file or folder we will delete them. To implement that logic, use the below script.
gci C:\Temp -Recurse | foreach { if($_.Length -eq 0){ Write-Output "Removing Empty File $($_.FullName)" $_.FullName | Remove-Item -Force } if( $_.psiscontainer -eq $true){ if((gci $_.FullName) -eq $null){ Write-Output "Removing Empty folder $($_.FullName)" $_.FullName | Remove-Item -Force } }
The above command will remove empty files and folders/sub-folders from the C:\temp path.
Output
You will see the output something like this.
- Related Articles
- How to delete hidden files and folders using PowerShell?
- How to get hidden files and folders using PowerShell?
- How to retrieve files and folders attributes using PowerShell?
- How to change files and folders attributes using PowerShell?
- How to display files/folders including hidden files/folders in PowerShell?
- How to Zip / Unzip files or folders using PowerShell?
- How to get the list of empty folders using PowerShell?
- How to delete only empty folders in Python?
- How to get only hidden files and folders in PowerShell?
- How to copy readonly and hidden files/folders in the PowerShell?
- How to get empty files in Windows OS using PowerShell?
- How to delete all files and folders from a path in C#?
- How to get only files but not the folders with Get-ChildItem using PowerShell?
- How to remove hidden files and folders using Python?
- How to delete folder and sub folders using Java?

Advertisements