Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function global:Write-Verbose ( [string]$Message )
- # check $VerbosePreference variable, and turns -Verbose on
- { if ( $VerbosePreference -ne 'SilentlyContinue' )
- { Write-Host " $Message" -ForegroundColor 'Yellow' } }
- $VerbosePreference = "Continue"
- $LogDate = get-date -format "MM-d-yy-HH"
- $objShell = New-Object -ComObject Shell.Application
- $binFolder = $objShell.Namespace(0xA)
- $ErrorActionPreference = "SilentlyContinue"
- Start-Transcript -Path C:\Windows\Temp\$LogDate.log
- ## Cleans all code off of the screen.
- Clear-Host
- $size = Get-ChildItem C:\Users\* -Include *.iso, *.vhd -Recurse -ErrorAction SilentlyContinue |
- Sort Length -Descending |
- Select-Object Name, Directory,
- @{Name="Size (GB)";Expression={ "{0:N2}" -f ($_.Length / 1GB) }} |
- Format-Table -AutoSize | Out-String
- $Before = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName,
- @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
- @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
- @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) } },
- @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } |
- Format-Table -AutoSize | Out-String
- ## Stops the windows update service.
- Get-Service -Name wuauserv | Stop-Service -Force -Verbose -ErrorAction SilentlyContinue
- ## Windows Update Service has been stopped successfully!
- $DaysToDelete = 1
- $distribDir = "C:\Windows\SoftwareDistribution\*" ## Deletes the contents of windows software distribution.
- $windowsTempDir = "C:\Windows\Temp\*" ## Deletes the contents of the Windows Temp folder.
- $tempDir = "C:\Temp\*" ## Deletes all cookies.
- #$locSetDir = "C:\Documents and Settings\*\Local Settings\Temp\*" ## Delets all local settings temp
- #$locSetIEDir = "C:\Documents and Settings\*\Local Settings\Temporary Internet Files\*" ## Delets all local settings IE temp
- #$locSetHisDir = "C:\Documents and Settings\*\Local Settings\History\*" ## Delets all local settings history
- Get-ChildItem $distribDir, $windowsTempDir $tempDir -Recurse -Force -Verbose -ErrorAction SilentlyContinue |
- Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete)) } |
- remove-item -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue
- $targets = "AppData\Local\Temp",
- "AppData\Local\Microsoft\Windows\Temporary Internet Files",
- "AppData\Local\Microsoft\Windows\INetCache\",
- "AppData\Local\Microsoft\Windows\Caches"
- $special = @("SSHD", "Public", "Default User")
- $profiles = Get-ChildItem "C:\Users" -Directory -Force |
- Where-Object Name -NotIn $special |
- Select-Object -ExpandProperty FullName
- $profiles | ForEach-Object {
- foreach($target in $targets) {
- $path = Join-Path $_ $target
- remove-item $path -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue
- }
- }
- $DaysToDelete = 2
- $targets = "profile/Default",
- "profile/Profile 1",
- "profile/Profile 2",
- "profile/Profile 3",
- "profile/Profile 4"
- $special = @("chromium", "64")
- $profiles = Get-ChildItem "C:\" -Directory -Force |
- Where-Object Name -In $special |
- Select-Object -ExpandProperty FullName
- $chromeDir = "C:\Users\*\AppData\Local\Google\Chrome\User Data\Default"
- $chromeSetDir = "C:\Users\*\Local Settings\Application Data\Google\Chrome\User Data\Default"
- $Items = @("*Archived History*",
- "*Cache*",
- "*Cookies*",
- "*History*",
- "*Top Sites*",
- "*Visited Links*",
- "*Web Data*")
- $profiles | ForEach-Object {
- foreach($target in $targets) {
- $profile = Join-Path $_ $target
- $items | ForEach-Object {
- $item = $_
- Get-ChildItem $profile, $chromeDir, $chromeSetDir -Recurse -Force -ErrorAction SilentlyContinue |
- Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete)) -and $_.Directory -like $item} | ForEach-Object {
- $path = Join-Path $_.DirectoryName $_
- Remove-Item $path -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue }
- }
- }
- }
- ## deletes the contents of the recycling Bin.
- $binFolder.items() | ForEach-Object { Remove-Item $_.path -ErrorAction Ignore -Force -Verbose -Recurse }
- ## The Recycling Bin has been emptied!
- #$cleanmgr = get-childitem cleanmgr.exe | select -expandproperty lastaccesstime
- If ((Get-Date).Day%7 -eq 0) {
- ## Running Disk Clean up Tool
- write-Host "Finally now, Running Windows disk Clean up Tool" -ForegroundColor Cyan
- cleanmgr /sagerun:11 | out-Null
- $([char]7)
- Sleep 1
- $([char]7)
- Sleep 1
- }
- ## Starts the Windows Update Service
- ##Get-Service -Name wuauserv | Start-Service -Verbose
- $After = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName,
- @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
- @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
- @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) } },
- @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } |
- Format-Table -AutoSize | Out-String
- ## Sends some before and after info for ticketing purposes
- Write-Verbose "Before: $Before"
- Write-Verbose "After: $After"
- Write-Verbose $size
- Write-Verbose "Items failed to delete:"
- Write-Verbose $FailedItems | Foreach-Object {$_.CategoryInfo.TargetName}
- "Disk C distribution:"
- Get-ChildItem C:\* -Directory |
- Foreach {
- $stat = dir $_.FullName -File -Recurse |
- measure Length -Sum
- $_ | Add-Member -MemberType NoteProperty `
- -Name Files -Value $stat.count
- $_ | Add-Member -MemberType NoteProperty `
- -Name "Size (Gb)" -Value ("{0:N1}" -f( $stat.sum / 1gb )) -PassThru
- } | select -Last 10 | Sort FileSize | Select Name ,"Size (Gb)",Files |
- Format-Table -AutoSize | Out-String
- ## Completed Successfully!
- Get-Date | Select-Object DateTime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement