diff --git a/Microsoft/Run_Disk_Cleanup.ps1 b/Microsoft/Run_Disk_Cleanup.ps1 new file mode 100644 index 0000000..73c2d61 --- /dev/null +++ b/Microsoft/Run_Disk_Cleanup.ps1 @@ -0,0 +1,129 @@ +# Script by Timur@0x01337.com +# Date: 2023-11-24 +<# +.DESCRIPTION +Runs Disk Cleanup utility. Disk Cleanup will remove all types of junk files for all current logged users without user input. + +Requires administrator privileges. + +.PARAMETER help +Displays a detailed usage description of this script. + +.EXAMPLE +PS> .\Run-Disk-Cleanup.ps1 + +.EXAMPLE +PS> .\Run-Disk-Cleanup.ps1 -help +#> + +# Getting command line parameters +param ( + [parameter(Mandatory = $false)][switch]$help +) + +$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) +$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +if (!$isAdmin) { + Write-Error "This script requires administrator privileges" + exit 1 +} + +# Writing help message +if ($help) { + get-help $MyInvocation.MyCommand.Path -Full + exit 0 +} + +$registryKeyPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches' +# 2004 is just a magic number +$propertyName = "StateFlags2004" +#all availble clean options, ref: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cleanmgr +$allCleanOptions = "Active Setup Temp Folders", "D3D Shader Cache", "Delivery Optimization Files", +"Diagnostic Data Viewer database files", "Downloaded Program Files", "Internet Cache Files", +"Language Pack", "Old ChkDsk Files", "Recycle Bin", "RetailDemo Offline Content", +"Setup Log Files", "System error memory dump files", "System error minidump files", +"Temporary Files", "Thumbnail Cache", "Update Cleanup", "User file versions", +"Windows Defender", "Windows Error Reporting Files", "Temporary Setup Files" + +foreach ($option in $allCleanOptions) { + $path = "$registryKeyPath\$option" + + if (!(Test-Path $path)) { + Write-Host "Create registry key path ($path)" + New-Item -Path $path -Force + } + try { + Get-ItemProperty -Path $path -Name $propertyName -ErrorAction Stop | Out-Null + } + catch { + Write-Host "Creating property ($propertyName) in ($path)" + New-ItemProperty -Path $path -Name $propertyName -PropertyType DWORD -Value 2 + } + try { + Set-ItemProperty -Path $path -Name $propertyName -Value 2 + } + catch { + Write-Error $_ + exit 1 + } +} + +$command = "cleanmgr" +# 2004 is just a magic number +$arguments = "/sagerun:2004" + +$computerName = "$($env:COMPUTERNAME)" +if (!$computerName) { + Write-Error "Can't get computer name" + exit 1 +} + +$enabledUsers = @() +$users = Get-LocalUser +foreach ($user in $users) { + if ($user.Enabled) { + Write-Host "$($user.Name) found" + $enabledUsers += $user.Name + } +} + +foreach ($enabledUser in $enabledUsers) { + $userId = "$computerName\$enabledUser" + $taskName = "cleanmgr-$enabledUser" + + $task = $null + try { + $task = Get-ScheduledTaskInfo -TaskName $taskName -ErrorAction Stop + } + catch { + + } + + if (!$task) { + Write-Host "Create task '$taskName' for $userId" + try { + $action = New-ScheduledTaskAction -Execute $command -Argument $arguments + $principal = New-ScheduledTaskPrincipal -UserId $userId + $task = New-ScheduledTask -Action $action -Principal $principal + Register-ScheduledTask -TaskName $taskName -InputObject $task -ErrorAction Stop + } + catch { + #aovid access denied error + Write-Host "No access to create '$taskName' for $userId" + continue + } + } + + Write-Host "Running $command $arguments on user($userId)" + + try { + Start-ScheduledTask -TaskName $taskName -ErrorAction Stop + } + catch { + #aovid access denied error + Write-Host "No access to edit '$taskName' for $userId" + continue + } +} + +exit 0