Hello , i have a below script which is working very fine but now i need to EXCLUDE some files which is started with prefix PHONE, PIN, SMS how can i do that.
Below Script will Delete all child folders and contents in a directory that are older than 30 days also and added in the two lines to stop it deleting any child folders named "webserver" and "installer". apart from this i want to excule some of the file as mentioned above.
Path C:\Program Files (x86)\RIM\BES\Logs"
Folder A - will be deleted as mentioned
Folder B - will be deleted as mentioned
webserver - exculded from deletion
installer - excluled from deletion
Now the files with prefix with phone, SMS, Call under Folder A & B should not be deleted. how can i achive that.
---------------------------------
$Today = Get-Date
$DaysToKeep = "30"
$TargetFolder = "C:\Program Files (x86)\RIM\BES\Logs"
$LastWrite = $Today.AddDays(-$DaysToKeep)
$FldrExclusion1 = "webserver"
$FldrExclusion2 = "installer"
$Folders = Get-ChildItem -path $TargetFolder |
Where {$_.psIsContainer -eq $true} |
Where {$_.LastWriteTime -le "$LastWrite"} |
Where {$_.name -ne $FldrExclusion1} |
Where {$_.name -ne $FldrExclusion2}
foreach ($Folder in $Folders)
{
if ($Folder -eq $null) {
throw "No further folders to delete"
}
else {
write-host "Deleting $Folder.FullName" -foregroundcolor "Red"
Remove-Item $Folder.FullName -recurse -Confirm:$false
}
}