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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more