Skip to content

Instantly share code, notes, and snippets.

@terasaka
Last active August 28, 2018 21:21
Show Gist options
  • Save terasaka/6a0df01f688f5e87c434dc6af0a59bc4 to your computer and use it in GitHub Desktop.
Save terasaka/6a0df01f688f5e87c434dc6af0a59bc4 to your computer and use it in GitHub Desktop.
Script para limpeza de pastas temporárias. Lista as pastas de todos os usuários em C:\Users e limpa os diretórios definidos na variável.
$FolderToDelete = @("Folder1\To\Clean","Folder2\To\Clean")
$SpaceAtStart = (Get-WmiObject win32_logicaldisk | where { $_.DeviceID -eq $env:SystemDrive }).FreeSpace/1GB
$ProfileDirs = @()
Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | ForEach-Object {
$UserProfile = (Get-ItemProperty -Path $_.PSPath).ProfileImagePath
if ($Userprofile.substring(0, $($env:windir).length) -eq $env:windir) {
# Skipping - Profile in Windows Folder
} elseif (!(Get-ChildItem $UserProfile -ErrorAction SilentlyContinue)) {
# Skipping - Folder does not exist"
} else {
$ProfileDirs += $UserProfile
}
}
ForEach($Folder in $FolderToDelete){
ForEach($Profile in $ProfileDirs)
{
if (Test-Path $Profile\$Folder) {
Remove-Item -Path $Profile\$Folder\* -Recurse -Force >$null 2>&1
}
else {
Write-Host "$Profile - Folder not exist!"
}
}
}
$CurrentSpace = (Get-WmiObject win32_logicaldisk | where { $_.DeviceID -eq $env:SystemDrive }).FreeSpace/1GB
$Savings = [Math]::Round($CurrentSpace - $SpaceAtStart)
$Message = @"
Starting Free Space: $SpaceAtStart
Current Free Space: $CurrentSpace
Savings: $Savings GB
Exiting.
"@
Write-Host "-----------------------------------------------------"
Write-Host $Message
Write-Host "-----------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment