Skip to content

Instantly share code, notes, and snippets.

@MattJeanes
Forked from mikesigs/DeleteExcludedFiles.ps1
Last active September 6, 2018 17:21
Show Gist options
  • Save MattJeanes/6d1fd9041d1c09ded8199781e005ffe0 to your computer and use it in GitHub Desktop.
Save MattJeanes/6d1fd9041d1c09ded8199781e005ffe0 to your computer and use it in GitHub Desktop.
PowerShell Script to Find (and delete) all excluded files in a Visual Studio Solution
$Solution = "C:\_git\Mercury\Mercury.sln"
[switch]$DeleteFromDisk = $true
$ErrorActionPreference = "Stop"
$solutionDir = Split-Path $Solution | % { (Resolve-Path $_).Path }
cd $solutionDir
$projects = Select-String -Path $Solution -Pattern 'Project.*"(?<file>.*\.csproj)".*' `
| % { $_.Matches[0].Groups[1].Value } `
| % { Join-Path $solutionDir $_ } `
| Where { -not ( (Get-Content $_) -Match "<Project .*Microsoft.NET.Sdk") }
$projects
$excluded = $projects | % {
$projectDir = Split-Path $_
#"projectDir: $projectDir"
$projectFiles = Select-String -Path $_ -Pattern '<(TypeScriptCompile|Compile|None|Content|EmbeddedResource) Include="(.*)"' `
| % { $_.Matches[0].Groups[2].Value } `
| % { Join-Path $projectDir $_ } `
#"Project files"
#$projectFiles
$diskFiles = Get-ChildItem -Path $projectDir -Recurse `
| ? { !$_.PSIsContainer } `
| % { $_.FullName } `
| ? { $_ -notmatch "\\obj\\|\\bin\\|\\logs\\|\.user$|\.*proj$|\.*log$|App_Configuration\\|App_Data\\|node_modules\\|lib\\|bower_components\\|Configs\\" } `
#$diskFiles
(compare-object -ReferenceObject @($diskFiles) -DifferenceObject @($projectFiles) -PassThru) | Where { $_.SideIndicator -eq '<=' }
}
Write-Host "Found" $excluded.count "excluded files"
if($DeleteFromDisk)
{
Write-Host "Deleting excluded files from disk..."
$excluded | % { Remove-Item -Path $_ -Force -Verbose}
}
else
{
Write-Host "DeleteFromDisk was not specified. Listing excluded files only..."
$excluded
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment