Clean Bin And Obj Folders Recursively In Code Base Folder

Often we run into issues where we have a lot of folders created for different code solutions in .net. We may have to clean the bin and obj folder, which are not needed to make some space on the disk. This can be achieved with the simple PowerShell command below,

Get-ChildItem . -Recurse | Where-Object {$_.PSIsContainer -eq $true -and ($_.Name -match "bin") -or ($_.Name -match "obj")} | Remove-Item -Force -Recurse

Use this command with caution to delete all the bin and obj folders. This command finds all the bin and obj folders under the main folder where it runs and deletes them by force without confirmation.

Get-ChildItem

It gets all the child items, and the -Recurse parameter specifies to get sub-folders also.

The output of this command is passed to Where-Object comm, where we check for the folder names and filter for "bin" and "obj" folders. 

Once filtered, the next step is to pass the filtered output to the Remove-Item command. Which then deletes the folder and its contents. Recurse and Force keywords ensure that all the files inside these folders are deleted without the confirmation "Yes/No" prompt.