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)