I have a PowerShell script that recursively deletes all files and folders, but excludes certain folders as they should not be deleted. It works 100%, but my problem is performance. I need this to run a lot faster.
Any ideas on how to make this faster?
Write-Host "Purging $InstallationDirectorySite - Deleting files..."
$FolderExlusions = (
"App_Data",
"Logs",
"TEMP",
"ExamineIndexes",
"DistCache",
"GitPathProviderRepository"
)
[regex] $files_regex = "Logs|ExamineIndexes|DistCache*|GitPathProviderRepository*"
if(Test-Path $InstallationDirectorySite) {
Get-ChildItem -Path $InstallationDirectorySite -Recurse -Exclude $FolderExlusions |
Where-Object {$_.FullName -notmatch $files_regex} |
Remove-Item -Recurse
}
else {
Write-Output "$InstallationDirectorySite doesn't exist"
}