DEV Community

redhcp
redhcp

Posted on

Powershell - Remove files - folders πŸ“‚

These scripts make it easy to remove files or folders.

REMOVE BY EXTENSION, YOU CAN EXCLUDE

Get-ChildItem -Recurse -Path 'C:\YOUR-PATH'  -Exclude *.pdf , *.mp4 | Remove-Item -force -Verbose -WhatIf
Enter fullscreen mode Exit fullscreen mode

DELETE FOLDERS EMPTY RECURSE

$path="C:\YOUR-PATH\*"
Get-ChildItem $path -Recurse -Force -Directory | 
    Sort-Object -Property FullName -Descending |
    Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } |
    Remove-Item -Verbose -WhatIf
Enter fullscreen mode Exit fullscreen mode

Parameters:

  • -Verbose: to provide status updates, detailed tracking information execution by console.
  • -WhatIf: for TEST before execute without changes.
  • -Recurse: execute cmd recursively.
  • -Exclude: for exclude folders, extensions.

Thanks for read!

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay