Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #PowerShell script to clean out temporary folders
- #Load Active Directory PowerShell Module
- Import-Module activedirectory
- #Variable array for folders to clean, others can be added as needed
- $tempRM = @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*")
- $tempUNC = @("\\$computer\c$\Windows\Temp\*", "\\$computer\c$\Windows\Prefetch\*", "\\$computer\c$\Documents and Settings\*\Local Settings\temp\*", "\\$computer\c$\Users\*\Appdata\Local\Temp\*")
- #AD Path to search for computers to clean
- $searchbase = "OU=derp,OU=Computers,OU=derp,DC=domain,DC=com"
- #Get the list of computers to clean
- $query = Get-ADComputer -Filter * -SearchBase $searchbase | Select DNSHostName | Sort DNSHostName
- $computers = $query.DNSHostName
- #Operation to clear out temp files in specified folders
- #WinRM method requires winrm quickconfig -q to be run on host machines first, psexec command above should take care of that
- #UNC method requires the credentials provided above to have access to admin shares
- Try {
- foreach ($computer in $computers)
- {
- Write-Host "$computer - Connecting..."
- if(Test-Connection $computer -Count 1 -Quiet)
- {
- Write-Host -ForegroundColor Green "$computer - Deleting files!"
- Invoke-Command -ComputerName $computer -ScriptBlock { foreach ($folder in $using:tempRM) {Remove-Item -Path $folder -Recurse -Force -ErrorAction SilentlyContinue} }
- Write-Host -ForegroundColor Green "$computer - Done!"
- }
- else
- {
- Write-Host -ForegroundColor Red "$computer - Offline!"
- }
- }
- }
- Catch {
- foreach ($computer in $computers) {
- Write-Host -ForegroundColor Green "$computer - Deleting files!"
- Remove-Item -path $tempUNC -force -recurse
- Write-Host -ForegroundColor Green "$computer - Done!"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment