Skip to content

Instantly share code, notes, and snippets.

@cezarypiatek
Last active February 12, 2019 20:17
Show Gist options
  • Save cezarypiatek/4881ceef6fc8c439ab7611c41133144b to your computer and use it in GitHub Desktop.
Save cezarypiatek/4881ceef6fc8c439ab7611c41133144b to your computer and use it in GitHub Desktop.
Script for detecting orphaned files in C# projects
function Search-ItemsRecursive{
[CmdletBinding()]
param($ProjectItems, $Filter, [switch]$Recurse=$false)
foreach ($item in $ProjectItems) {
if($(. $Filter $item))
{
$item
}
if(($item.ProjectItems -ne $null) -and $Recurse){
Search-ItemsRecursive -ProjectItems $item.ProjectItems -Filter $Filter -Recurse:$Recurse
}
}
}
function Find-OrphanFilesInSolution([switch]$IgnoreObj=$true){
Get-Project -All | Find-OrphanFilesInProject -IgnoreObj:$IgnoreObj
}
function Find-OrphanFilesInProject{
[CmdletBinding()]
param([Parameter(ValueFromPipeline=$true)]$Projects, [switch]$IgnoreObj=$true)
process{
$toProcess = if($Projects){$Projects}else{Get-Project}
foreach($p in $toProcess){
$projectPath = Split-Path $p.FullName -Parent
$csFilesInProject = Search-ItemsRecursive -ProjectItems $p.ProjectItems -Filter {param($item)$item.Name -like "*.cs"} -Recurse |% {$_.Properties.Item("FullPath").Value}
Get-ChildItem $projectPath -Filter "*.cs" -Recurse |? { $csFilesInProject -notcontains $_.FullName } |? {(-not $IgnoreObj) -or ($_.Directory -notlike "*\obj*")} |select FullName
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment