Over a million developers have joined DZone.

Powershell Recipe 1: Clean up binaries before build

DZone 's Guide to

Powershell Recipe 1: Clean up binaries before build

· ·
Free Resource

In my mind two great inventions Microsoft came in last ten years were Windows Presentation Foundations (also Slilverlight) and Powershell. We in .NET used to jealous of Linux Shell Programming as something that powerful was not possible in Windows. Powershell changed all that, at present it is the most powerful scripting language and shell available ( btw: Its 10 times better than linux shells even).

I use PS (PowerShell) in everyday repetitive tasks. We use it to download source code from source code control, build code, release code, create release notes, email to sqa, mark task as fixed in task management system, run automated test scripts and in many more tasks. From now on I am going to share a few PS recipes that make my life easier on a regular basis. I use it as much as I use C#.

When doing automated build, its best to clear the old binaries, or even its good to clear the binary files when running from VS because old binaries can sometimes create unpredictable results.

Cleaning Up Binaries in folder

# Returns a list of files given the filter pattern
# $sourcePath - Path which to recursively look for files
# $pattern – File filter such as *.dll
# $list – Ignore, used internally

function Get-Files ( $sourcePath, $pattern, $list )
# Is this the root call
$return_list = $false
if ($list -eq $null)
# Create the collection that we will return
$list = New-Object System.Collections.ObjectModel.Collection[System.IO.FileInfo]
$return_list = $true;

$dir = New-Object System.IO.DirectoryInfo $sourcePath
$files = $null
$files = $dir.GetFiles($pattern)
if ($files.Length -gt 0)
$files | % {$list.Add($_)}
$dirs = $dir.GetDirectories();
$dirs | % {Get-Files $_.FullName $pattern $list}
if ($return_list -eq $true)
return $list

Now we want to delete the dll and pdb files. We will create a function called Delete-Binaries. Get file function will return the list of files of a certain type. Then we can delete the files. See below

function Delete-Binaries ( $sourcePath )
Write-Host "Deleting all dll and pdb files from path $sourcePath"
Get-Files $sourcePath "*.dll" | % { $_.Delete() }
Get-Files $sourcePath "*.pdb" | % { $_.Delete() }
Write-Host "Deletion Complete"



Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}