þÿ########################################################## # # Script to delete temporary files. # # Required parameter: -Path (positional too) # path of directory containing files to be deleted # ########################################################## Param( [Parameter(Position=0, Mandatory=$true)] [string]$path ) $rundate = Get-Date $logfile = ".\DeleteDirectoryContents.log" $msgSuccess = "$rundate - Contents of $path deleted." $msgFailProtected = "$rundate - Path $path contains protected directory. Deletion terminated." $msgPathNotExist = "$rundate - Path $path does not exist." function WriteLog([string]$logentry) { Out-File -FilePath $logfile -InputObject $logentry -Append $logfile.Close } switch -wildcard ($path){ # Excludes common directories that should not be deleted "C:\" {WriteLog($msgFailProtected); break} "\" {WriteLog($msgFailProtected); break} "*.\*" {WriteLog($msgFailProtected); break} "\\*" {WriteLog($msgFailProtected); break} "*..\*" {WriteLog($msgFailProtected); break} "env*" {WriteLog($msgFailProtected); break} "HK*" {WriteLog($msgFailProtected); break} "*Win*" {WriteLog($msgFailProtected); break} "*Program*" {WriteLog($msgFailProtected); break} "*Users*" {WriteLog($msgFailProtected); break} "*Inetpub*" {WriteLog($msgFailProtected); break} "*IIS*" {WriteLog($msgFailProtected); break} "*Perflogs*" {WriteLog($msgFailProtected); break} default {if(Test-Path $path){ Remove-Item -Path $path\*.* -Recurse -Force; WriteLog($msgSuccess) } else{ WriteLog($msgPathNotExist) } } }