PowerShell script for cleaning Visual Studio Project build directories (e.g.: bin/ and obj/) from a particular path, recursively.

$script:scriptDir = Split-Path $script:MyInvocation.MyCommand.Path

function Write-Info
{
    param(
        [String] $Message
    )

    Write-Host "$Message" -ForegroundColor Cyan
}

function Write-Success
{
    param(
        [String] $Message
    )

    Write-Host "$Message" -ForegroundColor Green
}

function Delete-If-Exists
{
    param(
        [string] $dir
    )

    if (!(Test-Path -Path $dir))
    {
        Write-Host "Path does not exist '$dir'"
    }
    else
    {
        Remove-Item "$dir" -recurse -Force
        Write-Host "Deleted '$dir'"
    }
}


function Main
{
    # cd to project root dir:
    Push-Location -Path "$script:scriptDir"

    #
    # Delete each Visual Studio project's build files/dirs
    # e.g.: src/.../bin/ and src/.../obj/
    $vsProjects = Get-ChildItem .\src\ | where { $_.Attributes -eq 'Directory' }
    foreach ($vsProject in $vsProjects)
    {
        Write-Info "> Cleaning '$vsProject'"
        $vsProjectPath = ".\src\$vsProject"
        Delete-If-Exists "$vsProjectPath\bin\"
        Delete-If-Exists "$vsProjectPath\obj\"
        Write-Host
    }

    Pop-Location
}

Main

if ($? -eq $true)
{
    Write-Success "Finished."
}
else
{
    Write-Error "Finished, with errors."
}


# --------------------------------------------------------------------------
# Usage
# --------------------------------------------------------------------------

# Create a `clean.cmd` file in the root of your vs project directory to call
# this PowerShell script, and to delete all 'src/PROJECT_NAME/bin/' and
# 'src/PROJECT_NAME/obj/' files and dirs, recursively.
#
# `Copy the lines below to your clean.cmd` file:
@echo off
powershell.exe -noprofile -executionpolicy bypass -command "& '%~dpn0.ps1'"