Created
July 26, 2021 18:14
-
-
Save LanceMcCarthy/02313c4d955fe1fe55dfba434480b8c9 to your computer and use it in GitHub Desktop.
Clean up bin obj packages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to also clean up node projects, add
node_packages
to the string matches.Change line 5 to
Change line 8 to