How to Delete Folder Contents with Exclusions in PowerShell
Автор: vlogize
Загружено: 2025-10-16
Просмотров: 2
Описание:
Discover a step-by-step guide on using PowerShell to delete folder contents while keeping certain files safe. Learn how to sort and manage folder contents effectively.
---
This video is based on the question https://stackoverflow.com/q/67738371/ asked by the user 'Carroll' ( https://stackoverflow.com/u/1226585/ ) and on the answer https://stackoverflow.com/a/67738670/ provided by the user 'Civette' ( https://stackoverflow.com/u/10722100/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Powershell - Delete Folder Contents With Exclusions
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Delete Folder Contents with Exclusions in PowerShell
Managing folder contents efficiently is crucial for maintaining an organized file system, particularly when you want to automate cleanup tasks. One common scenario is needing to delete files within subfolders while keeping specific files intact based on certain criteria. In this guide, we will explore how to accomplish this task using PowerShell.
The Challenge
Imagine you have a parent folder filled with multiple subfolders containing various files. You want to:
Sort the folders based on the oldest last access time of any file within those subfolders.
Delete specific files while excluding others from deletion.
Keep track of the total size of the files that have been deleted.
Stop the script once you reach a certain deletion threshold.
Let’s break down how to implement these requirements using PowerShell.
Step-by-Step Solution
1. Setup Your Environment
Before diving into the script, ensure that you have PowerShell installed and ready for use. You will need to define the path to your target folder.
[[See Video to Reveal this Text or Code Snippet]]
Replace "Your entry folder path" with the actual path of the parent folder you want to manage.
2. Retrieve and Sort Folders
Next, you will want to list all directories within the specified path. You can do this with the Get-ChildItem cmdlet and sort them based on the last write time. Here’s how:
[[See Video to Reveal this Text or Code Snippet]]
3. Define Exclusions
To ensure you do not delete certain files, you can define a pattern for those exclusions. For instance, if you want to keep files with a specific extension or naming convention, modify the criteria in the Where-Object filter:
[[See Video to Reveal this Text or Code Snippet]]
Replace "Your pattern to keep" with the appropriate expression to match your exclusion criteria.
4. Deleting Files
Once you’ve filtered the files you want to delete, the next step is to remove them. You can accomplish this by piping the results to the Remove-Item cmdlet. Here’s the complete command to delete the unwanted files:
[[See Video to Reveal this Text or Code Snippet]]
5. Monitor Deletion Size
To keep a running total of the size of deleted files, you can introduce a variable at the beginning of your script:
[[See Video to Reveal this Text or Code Snippet]]
Then, modify your delete command to calculate and add to this total:
[[See Video to Reveal this Text or Code Snippet]]
6. Exiting Upon Threshold
Finally, after you implement the above changes, incorporate a condition to exit the script once a specific size threshold is met.
[[See Video to Reveal this Text or Code Snippet]]
Adjust 100MB to whatever threshold is suitable for your needs.
Conclusion
With this PowerShell guide, you now have a clear view of how to delete folder contents with exclusions. By following the steps outlined, you can efficiently manage your file system while ensuring that important data remains untouched.
Feel free to adapt the scripts to best fit your needs and always test in a safe environment to avoid accidental data loss.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: