Last active
May 18, 2022 00:12
-
-
Save bryanknox/6256196f98c1409b350827e0c8c713bb to your computer and use it in GitHub Desktop.
PowerShell script module file to remove all bin and obj folders under the current folder and any subfolders. Handy for Visual Studio projects and solutions.
This file contains 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
# Remove all bin and obj folders under the current folder and any subfolders. | |
# | |
# This is handy for cleaning up bin and obj folders in Visual Studio projects and solutions. | |
function Remove-BinObjFolders { | |
param( | |
[switch]$Verbose, | |
[switch]$WhatIf | |
) | |
$currentFolderPath = Get-Location | |
if ($Verbose) { | |
Write-Host "Removing any bin and obj folders under: $currentFolderPath" | |
} | |
$pathList = Get-ChildItem $currentFolderPath -include bin,obj -Recurse | |
if ($Verbose) { | |
Write-Host "Found $($pathList.Length) paths to remove." | |
} | |
foreach ($path in $pathList) { | |
if ($Verbose) { | |
Write-Host "Removing: $path" | |
} | |
if (-not $WhatIf) { | |
Remove-Item $path -Force -Recurse | |
} | |
} | |
} | |
Export-ModuleMember -Function Remove-BinObjFolders |
This file contains 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
# Test of Remove-BinObjFolders function. | |
$ErrorActionPreference = "Stop" | |
Write-Host | |
Write-Host "----------------------" | |
# Arrange | |
Write-Host | |
Write-Host "Arrange" | |
$testPaths =@( | |
"./Test1/bin", | |
"./Test1/obj", | |
"./Test2/Test2a/bin", | |
"./Test2/Test2a/obj", | |
"./Test3/Test3a/Test3ab/bin", | |
"./Test3/Test3b/Test2bc/obj" | |
) | |
foreach ($path in $testPaths) | |
{ | |
Write-Host $path | |
if (-not (Test-Path $path)) { | |
Write-Host "Creating test path: $path" | |
New-Item $path -ItemType Directory | out-null | |
} | |
} | |
# Act | |
Write-Host | |
Write-Host "Act" | |
Remove-BinObjFolders -Verbose # -WhatIf | |
# Assert | |
Write-Host | |
Write-Host "Assert" | |
$errorCount = 0 | |
foreach ($path in $testPaths) | |
{ | |
if (Test-Path $path) { | |
Write-Host "Error! path not removed: $path" -ForegroundColor "Red" | |
$errorCount = $errorCount + 1 | |
} | |
} | |
if ($errorCount -eq 0) { | |
Write-Host "Passed! All paths removed!" -ForegroundColor "Green" | |
} | |
# Done | |
Write-Host | |
Write-Host "Done." | |
Write-Host |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment