Skip to content

Instantly share code, notes, and snippets.

@jknopp
Created August 16, 2019 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jknopp/e1192bb45d13b3d8ea8299c4f5c39aa0 to your computer and use it in GitHub Desktop.
Save jknopp/e1192bb45d13b3d8ea8299c4f5c39aa0 to your computer and use it in GitHub Desktop.
Finds all csproj in the supplied path and removes all the duplicate entries
<#
.SYNOPSIS
Finds all csproj in the supplied path and removes all the duplicate entries.
.DESCRIPTION
This script finds all duplicate <content> and <compile> entries in csproj files,
and removes them.
.NOTES
File Name : RemoveCsProjDuplicates.ps1
Author : Rodrigo F. Fernandes - github.com/rodrigoff
.LINK
https://github.com/rodrigoff/powershell/blob/master/csproj-utils/RemoveCsProjDuplicates.ps1
#>
Param(
[string]$filePath = $(throw "You must supply a file path")
)
$filePath = Resolve-Path $filePath
"> Searching for project files in $filePath"
$projectFiles = Get-ChildItem -Path $filePath -Include *.csproj -Recurse `
| Where-Object { $_.FullName -notmatch "\\packages\\?" } `
| Select-Object -ExpandProperty FullName
"> Found $($projectFiles.Count) project files"
Foreach($projectFile in $projectFiles) {
$xml = [xml] (Get-Content $projectFile)
Write-Host "> $projectFile " -Foreground Green
$entries = $xml.Project.ItemGroup.Compile + $xml.Project.ItemGroup.Content | Group-Object Include
$duplicateEntries = $entries | Where-Object Count -gt 1
"- Found $($duplicateEntries.Count) duplicate entries"
if (!$duplicateEntries) {
continue
}
foreach ($duplicateEntry in $duplicateEntries) {
While ($duplicateEntry.Group.Count -gt 1) {
$e = $duplicateEntry.Group[0]
$e.ParentNode.RemoveChild($e) | Out-Null
$duplicateEntry.Group.Remove($e) | Out-Null
}
}
$xml.Save($projectFile) | Out-Null
"- Removed $($duplicateEntries.Count) duplicate entries"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment