Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Created July 26, 2021 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LanceMcCarthy/02313c4d955fe1fe55dfba434480b8c9 to your computer and use it in GitHub Desktop.
Save LanceMcCarthy/02313c4d955fe1fe55dfba434480b8c9 to your computer and use it in GitHub Desktop.
Clean up bin obj packages
# PowerShell script that recursively deletes all 'bin' and 'obj' (or any other specified) folders inside current folder
$CurrentPath = (Get-Location -PSProvider FileSystem).ProviderPath
# recursively get all folders matching given includes, except ignored folders
$FoldersToRemove = Get-ChildItem .\ -include bin,obj,*.zip -Recurse | Where-Object {$_ -notmatch '_tools' -and $_ -notmatch '_build'} | ForEach-Object {$_.fullname}
# recursively get all folders matching given includes
$AllFolders = Get-ChildItem .\ -include bin,obj,*.zip -Recurse | ForEach-Object {$_.fullname}
# subtract arrays to calculate ignored ones
$IgnoredFolders = $AllFolders | Where-Object {$FoldersToRemove -notcontains $_}
# remove folders and print to output
if($null -ne $FoldersToRemove)
{
Write-Host
foreach ($item in $FoldersToRemove)
{
remove-item $item -Force -Recurse;
Write-Host "Removed: ." -nonewline;
Write-Host $item.replace($CurrentPath, "");
}
}
# print ignored folders to output
if($null -ne $IgnoredFolders)
{
Write-Host
foreach ($item in $IgnoredFolders)
{
Write-Host "Ignored: ." -nonewline;
Write-Host $item.replace($CurrentPath, "");
}
Write-Host
Write-Host $IgnoredFolders.count "folders ignored" -foregroundcolor yellow
}
# print summary of the operation
Write-Host
if($null -ne $FoldersToRemove)
{
Write-Host $FoldersToRemove.count "folders removed" -foregroundcolor green
}
else
{
Write-Host "No folders to remove" -foregroundcolor green
}
Write-Host
# prevent closing the window immediately
$dummy = Read-Host "Completed, press enter to continue."
@LanceMcCarthy
Copy link
Author

LanceMcCarthy commented Oct 5, 2021

If you want to also clean up node projects, add node_packages to the string matches.

Change line 5 to

$FoldersToRemove = Get-ChildItem .\ -include bin,obj,*.zip, node_packages -Recurse   | Where-Object {$_ -notmatch '_tools' -and $_ -notmatch '_build'} | ForEach-Object {$_.fullname}

Change line 8 to

$AllFolders = Get-ChildItem .\ -include bin,obj,*.zip, node_packages -Recurse | ForEach-Object {$_.fullname}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment