Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ProgressPreference="SilentlyContinue"
- $ErrorActionPreference = "Continue"
- for ([byte]$c = [char]'A'; $c -le [char]'Z'; $c++)
- {
- $variablePath = [char]$c + ':\variables.ps1'
- if (test-path $variablePath) {
- . $variablePath
- break
- }
- }
- # List installed updates
- Write-Host "Listing installed updates"
- Get-Hotfix
- #Detect O/S and use appropriate Dism command
- if ($RunDism){
- $RunDismCount = 2
- While ($RunDismCount -gt 0) {
- try{
- $osName = (Get-WmiObject win32_operatingsystem).caption
- switch -wildcard ($osName)
- {
- "*Server*2012 Standard" {Write-Host "Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp"; Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp}
- "*Server*2012 Datacenter" {Write-Host "Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp"; Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp}
- default {Write-Host "Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase"; Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase }
- }
- $RunDismCount = $RunDismCount - 1
- } catch {
- Write-Host "Unable to clean image."
- $RunDismCount = $RunDismCount - 1
- }
- }
- # List installed updates
- Write-Host "Listing installed updates"
- Get-Hotfix
- } else {
- Write-Host "Skipping Dism cleanup"
- }
- if ($SkipCleanup){
- Write-Host "Skipping cleanup"
- exit 0
- }
- # Remove 7-zip
- $version = $ver7zip
- $msi_file_name = "7z$version-x64.msi"
- $argumentList = '/x "C:\Windows\Temp\' + $msi_file_name + '" /qn'
- Write-Host "Removing 7-zip if it exists"
- $process = Start-Process -FilePath "msiexec" -ArgumentList $argumentList -NoNewWindow -PassThru -Wait
- $process.ExitCode
- #Clean up temp files
- Write-Host "Removing temp folders"
- $tempfolders = @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*")
- Remove-Item $tempfolders -ErrorAction SilentlyContinue -Force -Recurse
- #Sdelete - clean disk
- $wsusService = Get-Service -Name wuauserv
- $bitsService = Get-Service -Name bits
- #stop bits,wuauserv services (required)
- if ($wsusService.Status -eq 'Running') {
- Write-Host "wsus is running"
- Stop-Service $wsusService}
- if ($bitsService.Status -eq 'Running') {
- Write-Host "bits is running"
- Stop-Service $bitsService}
- Start-Sleep -s 10
- #remove the contents only of software distribution directory
- Remove-Item -Recurse -Force C:\Windows\SoftwareDistribution\*
- #accept license agreement
- New-Item -Path "HKCU:\Software\Sysinternals" -name Sdelete -Force
- # create reg path and key
- New-ItemProperty -Path "HKCU:\Software\Sysinternals\Sdelete" -Name "EulaAccepted" -Value "1" -PropertyType "Dword"
- # edit path/create/key with a value
- #run sDelete with switches and wait to complete (runs for 5-10 mins)
- Start-process -filepath C:\sDelete\sdelete64.exe -ArgumentList "-q", "-z", "c:" -Wait
- start-sleep -s 10
- #remove sdelete from the system
- Remove-Item -Recurse -Force C:\sdelete
- #it is not required to start wuauserv as it is set to manual and bits is set to auto
- #Clean Windows Update Registry.
- #Delete Windows Update keys, look for AccountDomainSid, SusClientId and AUOptions specifically, if keys are found - delete.
- Write-Host "Clean Windows Update Registry"
- Try {
- Remove-Item -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate' -Recurse
- }
- Catch
- {
- Write-Host "WindowsUpdate registry path does not exist, nothing to delete."
- }
- Try {
- Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name AccountDomainSid -ErrorAction Stop
- Write-Host "AccountDomainSid registry key deleted."
- }
- Catch {
- Write-Host "AccountDomainSid registry key does not exist, nothing to delete."
- }
- Try {
- Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name SusClientId -ErrorAction Stop
- Write-Host "SusClientId registry key deleted."
- }
- Catch {
- Write-Host "SusClientId registry key does not exist, nothing to delete."
- }
- Try {
- Remove-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name AUOptions -ErrorAction Stop
- Write-Host "AUOptions registry key deleted."
- }
- Catch {
- Write-Host "AUOptions registry key does not exist, nothing to delete."
- }
- #Add default Windows Update keys
- REG ADD "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" /v DisableWindowsUpdateAccess /t REG_DWORD /d 00000001 /f
- REG ADD "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" /v ElevateNonAdmins /t REG_DWORD /d 00000001 /f
- REG ADD "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" /v AUOptions /t REG_DWORD /d 00000003 /f
- #Clean Event Logs
- Write-Host "Clear Event Logs"
- wevtutil el | Foreach-Object {wevtutil cl "$_"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement