SHOW:
|
|
- or go back to the newest paste.
1 | Function Start-Cleanup { | |
2 | <# | |
3 | .SYNOPSIS | |
4 | Automate cleaning up a C:\ drive with low disk space | |
5 | ||
6 | .DESCRIPTION | |
7 | Cleans the C: drive's Window Temperary files, Windows SoftwareDistribution folder, | |
8 | the local users Temperary folder, IIS logs(if applicable) and empties the recycling bin. | |
9 | All deleted files will go into a log transcript in $env:TEMP. By default this | |
10 | script leaves files that are newer than 7 days old however this variable can be edited. | |
11 | ||
12 | .EXAMPLE | |
13 | PS C:\> .\Start-Cleanup.ps1 | |
14 | Save the file to your hard drive with a .PS1 extention and run the file from an elavated PowerShell prompt. | |
15 | ||
16 | .NOTES | |
17 | This script will typically clean up anywhere from 1GB up to 15GB of space from a C: drive. | |
18 | ||
19 | .FUNCTIONALITY | |
20 | PowerShell v3+ | |
21 | #> | |
22 | ||
23 | ## Allows the use of -WhatIf | |
24 | [CmdletBinding(SupportsShouldProcess=$True)] | |
25 | ||
26 | param( | |
27 | ## Delete data older then $daystodelete | |
28 | [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=0)] | |
29 | $DaysToDelete = 7, | |
30 | ||
31 | ## LogFile path for the transcript to be written to | |
32 | [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=1)] | |
33 | $LogFile = ("$env:TEMP\" + (get-date -format "MM-d-yy-HH-mm") + '.log'), | |
34 | ||
35 | ## All verbose outputs will get logged in the transcript($logFile) | |
36 | [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=2)] | |
37 | $VerbosePreference = "Continue", | |
38 | ||
39 | ## All errors should be withheld from the console | |
40 | [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=3)] | |
41 | $ErrorActionPreference = "SilentlyContinue" | |
42 | ) | |
43 | ||
44 | ## Begin the timer | |
45 | $Starters = (Get-Date) | |
46 | ||
47 | ## Check $VerbosePreference variable, and turns -Verbose on | |
48 | Function global:Write-Verbose ( [string]$Message ) { | |
49 | if ( $VerbosePreference -ne 'SilentlyContinue' ) { | |
50 | Write-Host "$Message" -ForegroundColor 'Green' | |
51 | } | |
52 | } | |
53 | ||
54 | ## Tests if the log file already exists and renames the old file if it does exist | |
55 | if(Test-Path $LogFile){ | |
56 | ## Renames the log to be .old | |
57 | Rename-Item $LogFile $LogFile.old -Verbose -Force | |
58 | } else { | |
59 | ## Starts a transcript in C:\temp so you can see which files were deleted | |
60 | Write-Host (Start-Transcript -Path $LogFile) -ForegroundColor Green | |
61 | } | |
62 | ||
63 | ## Writes a verbose output to the screen for user information | |
64 | Write-Host "Retriving current disk percent free for comparison once the script has completed. " -NoNewline -ForegroundColor Green | |
65 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
66 | ||
67 | ## Gathers the amount of disk space used before running the script | |
68 | $Before = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName, | |
69 | @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } }, | |
70 | @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f ( $_.Size / 1gb)}}, | |
71 | @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f ( $_.Freespace / 1gb ) } }, | |
72 | @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f ( $_.FreeSpace / $_.Size ) } } | | |
73 | Format-Table -AutoSize | | |
74 | Out-String | |
75 | ||
76 | ## Stops the windows update service so that c:\windows\softwaredistribution can be cleaned up | |
77 | Get-Service -Name wuauserv | Stop-Service -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Verbose | |
78 | ||
79 | # Sets the SCCM cache size to 1 GB if it exists. | |
80 | if ((Get-WmiObject -namespace root\ccm\SoftMgmtAgent -class CacheConfig) -ne "$null"){ | |
81 | # if data is returned and sccm cache is configured it will shrink the size to 1024MB. | |
82 | $cache = Get-WmiObject -namespace root\ccm\SoftMgmtAgent -class CacheConfig | |
83 | $Cache.size = 1024 | Out-Null | |
84 | $Cache.Put() | Out-Null | |
85 | Restart-Service ccmexec -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | |
86 | } | |
87 | ||
88 | ## Deletes the contents of windows software distribution. | |
89 | Get-ChildItem "C:\Windows\SoftwareDistribution\*" -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -recurse -ErrorAction SilentlyContinue -Verbose | |
90 | Write-Host "The Contents of Windows SoftwareDistribution have been removed successfully! " -NoNewline -ForegroundColor Green | |
91 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
92 | ||
93 | ## Deletes the contents of the Windows Temp folder. | |
94 | Get-ChildItem "C:\Windows\Temp\*" -Recurse -Force -Verbose -ErrorAction SilentlyContinue | | |
95 | Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays( - $DaysToDelete)) } | Remove-Item -force -recurse -ErrorAction SilentlyContinue -Verbose | |
96 | Write-host "The Contents of Windows Temp have been removed successfully! " -NoNewline -ForegroundColor Green | |
97 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
98 | ||
99 | ||
100 | ## Deletes all files and folders in user's Temp folder older then $DaysToDelete | |
101 | Get-ChildItem "C:\users\*\AppData\Local\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue | | |
102 | Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays( - $DaysToDelete))} | | |
103 | Remove-Item -force -recurse -ErrorAction SilentlyContinue -Verbose | |
104 | Write-Host "The contents of `$env:TEMP have been removed successfully! " -NoNewline -ForegroundColor Green | |
105 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
106 | ||
107 | ## Removes all files and folders in user's Temporary Internet Files older then $DaysToDelete | |
108 | Get-ChildItem "C:\users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" ` | |
109 | -Recurse -Force -Verbose -ErrorAction SilentlyContinue | | |
110 | Where-Object {($_.CreationTime -lt $(Get-Date).AddDays( - $DaysToDelete))} | | |
111 | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue -Verbose | |
112 | Write-Host "All Temporary Internet Files have been removed successfully! " -NoNewline -ForegroundColor Green | |
113 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
114 | ||
115 | ## Removes *.log from C:\windows\CBS | |
116 | if(Test-Path C:\Windows\logs\CBS\){ | |
117 | Get-ChildItem "C:\Windows\logs\CBS\*.log" -Recurse -Force -ErrorAction SilentlyContinue | | |
118 | remove-item -force -recurse -ErrorAction SilentlyContinue -Verbose | |
119 | Write-Host "All CBS logs have been removed successfully! " -NoNewline -ForegroundColor Green | |
120 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
121 | } else { | |
122 | Write-Host "C:\inetpub\logs\LogFiles\ does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
123 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
124 | } | |
125 | ||
126 | ## Cleans IIS Logs older then $DaysToDelete | |
127 | if (Test-Path C:\inetpub\logs\LogFiles\) { | |
128 | Get-ChildItem "C:\inetpub\logs\LogFiles\*" -Recurse -Force -ErrorAction SilentlyContinue | | |
129 | Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-60)) } | Remove-Item -Force -Verbose -Recurse -ErrorAction SilentlyContinue | |
130 | Write-Host "All IIS Logfiles over $DaysToDelete days old have been removed Successfully! " -NoNewline -ForegroundColor Green | |
131 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
132 | } | |
133 | else { | |
134 | Write-Host "C:\Windows\logs\CBS\ does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
135 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
136 | } | |
137 | ||
138 | ## Removes C:\Config.Msi | |
139 | if (test-path C:\Config.Msi){ | |
140 | remove-item -Path C:\Config.Msi -force -recurse -Verbose -ErrorAction SilentlyContinue | |
141 | } else { | |
142 | Write-Host "C:\Config.Msi does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
143 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
144 | } | |
145 | ||
146 | ## Removes c:\Intel | |
147 | if (test-path c:\Intel){ | |
148 | remove-item -Path c:\Intel -force -recurse -Verbose -ErrorAction SilentlyContinue | |
149 | } else { | |
150 | Write-Host "c:\Intel does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
151 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
152 | } | |
153 | ||
154 | ## Removes c:\PerfLogs | |
155 | if (test-path c:\PerfLogs){ | |
156 | remove-item -Path c:\PerfLogs -force -recurse -Verbose -ErrorAction SilentlyContinue | |
157 | } else { | |
158 | Write-Host "c:\PerfLogs does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
159 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
160 | } | |
161 | ||
162 | ## Removes $env:windir\memory.dmp | |
163 | if (test-path $env:windir\memory.dmp){ | |
164 | remove-item $env:windir\memory.dmp -force -Verbose -ErrorAction SilentlyContinue | |
165 | } else { | |
166 | Write-Host "C:\Windows\memory.dmp does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
167 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
168 | } | |
169 | ||
170 | ## Removes rouge folders | |
171 | Write-host "Deleting Rouge folders " -NoNewline -ForegroundColor Green | |
172 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
173 | ||
174 | ## Removes Windows Error Reporting files | |
175 | if (test-path C:\ProgramData\Microsoft\Windows\WER){ | |
176 | Get-ChildItem -Path C:\ProgramData\Microsoft\Windows\WER -Recurse | Remove-Item -force -recurse -Verbose -ErrorAction SilentlyContinue | |
177 | Write-host "Deleting Windows Error Reporting files " -NoNewline -ForegroundColor Green | |
178 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
179 | } else { | |
180 | Write-Host "C:\ProgramData\Microsoft\Windows\WER does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
181 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
182 | } | |
183 | ||
184 | ## Removes System and User Temp Files - lots of access denied will occur. | |
185 | ## Cleans up c:\windows\temp | |
186 | if (Test-Path $env:windir\Temp\) { | |
187 | Remove-Item -Path "$env:windir\Temp\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
188 | } else { | |
189 | Write-Host "C:\Windows\Temp does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
190 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
191 | } | |
192 | ||
193 | ## Cleans up minidump | |
194 | if (Test-Path $env:windir\minidump\) { | |
195 | Remove-Item -Path "$env:windir\minidump\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
196 | } else { | |
197 | Write-Host "$env:windir\minidump\ does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
198 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
199 | } | |
200 | ||
201 | ## Cleans up prefetch | |
202 | if (Test-Path $env:windir\Prefetch\) { | |
203 | Remove-Item -Path "$env:windir\Prefetch\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
204 | } else { | |
205 | Write-Host "$env:windir\Prefetch\ does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
206 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
207 | } | |
208 | ||
209 | ## Cleans up each users temp folder | |
210 | if (Test-Path "C:\Users\*\AppData\Local\Temp\") { | |
211 | Remove-Item -Path "C:\Users\*\AppData\Local\Temp\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
212 | } else { | |
213 | Write-Host "C:\Users\*\AppData\Local\Temp\ does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
214 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
215 | } | |
216 | ||
217 | ## Cleans up all users windows error reporting | |
218 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Windows\WER\") { | |
219 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Windows\WER\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
220 | } else { | |
221 | Write-Host "C:\ProgramData\Microsoft\Windows\WER does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
222 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
223 | } | |
224 | ||
225 | ## Cleans up users temporary internet files | |
226 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\") { | |
227 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
228 | } else { | |
229 | Write-Host "C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\ does not exist. " -NoNewline -ForegroundColor DarkGray | |
230 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
231 | } | |
232 | ||
233 | ## Cleans up Internet Explorer cache | |
234 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Windows\IECompatCache\") { | |
235 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Windows\IECompatCache\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
236 | } else { | |
237 | Write-Host "C:\Users\*\AppData\Local\Microsoft\Windows\IECompatCache\ does not exist. " -NoNewline -ForegroundColor DarkGray | |
238 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
239 | } | |
240 | ||
241 | ## Cleans up Internet Explorer cache | |
242 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Windows\IECompatUaCache\") { | |
243 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Windows\IECompatUaCache\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
244 | } else { | |
245 | Write-Host "C:\Users\*\AppData\Local\Microsoft\Windows\IECompatUaCache\ does not exist. " -NoNewline -ForegroundColor DarkGray | |
246 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
247 | } | |
248 | ||
249 | ## Cleans up Internet Explorer download history | |
250 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Windows\IEDownloadHistory\") { | |
251 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Windows\IEDownloadHistory\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
252 | } else { | |
253 | Write-Host "C:\Users\*\AppData\Local\Microsoft\Windows\IEDownloadHistory\ does not exist. " -NoNewline -ForegroundColor DarkGray | |
254 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
255 | } | |
256 | ||
257 | ## Cleans up Internet Cache | |
258 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Windows\INetCache\") { | |
259 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Windows\INetCache\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
260 | } else { | |
261 | Write-Host "C:\Users\*\AppData\Local\Microsoft\Windows\INetCache\ does not exist. " -NoNewline -ForegroundColor DarkGray | |
262 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
263 | } | |
264 | ||
265 | ## Cleans up Internet Cookies | |
266 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Windows\INetCookies\") { | |
267 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Windows\INetCookies\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
268 | } else { | |
269 | Write-Host "C:\Users\*\AppData\Local\Microsoft\Windows\INetCookies\ does not exist. " -NoNewline -ForegroundColor DarkGray | |
270 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
271 | } | |
272 | ||
273 | ## Cleans up terminal server cache | |
274 | if (Test-Path "C:\Users\*\AppData\Local\Microsoft\Terminal Server Client\Cache\") { | |
275 | Remove-Item -Path "C:\Users\*\AppData\Local\Microsoft\Terminal Server Client\Cache\*" -Force -Recurse -Verbose -ErrorAction SilentlyContinue | |
276 | } else { | |
277 | Write-Host "C:\Users\*\AppData\Local\Microsoft\Terminal Server Client\Cache\ does not exist. " -NoNewline -ForegroundColor DarkGray | |
278 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
279 | } | |
280 | ||
281 | Write-host "Removing System and User Temp Files " -NoNewline -ForegroundColor Green | |
282 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
283 | ||
284 | ## Removes the hidden recycling bin. | |
285 | if (Test-path 'C:\$Recycle.Bin'){ | |
286 | Remove-Item 'C:\$Recycle.Bin' -Recurse -Force -Verbose -ErrorAction SilentlyContinue | |
287 | } else { | |
288 | Write-Host "C:\`$Recycle.Bin does not exist, there is nothing to cleanup. " -NoNewline -ForegroundColor DarkGray | |
289 | Write-Host "[WARNING]" -ForegroundColor DarkYellow -BackgroundColor Black | |
290 | } | |
291 | ||
292 | ## Turns errors back on | |
293 | $ErrorActionPreference = "Continue" | |
294 | ||
295 | ## Checks the version of PowerShell | |
296 | ## If PowerShell version 4 or below is installed the following will process | |
297 | if ($PSVersionTable.PSVersion.Major -le 4) { | |
298 | ||
299 | ## Empties the recycling bin, the desktop recyling bin | |
300 | $Recycler = (New-Object -ComObject Shell.Application).NameSpace(0xa) | |
301 | $Recycler.items() | ForEach-Object { | |
302 | ## If PowerShell version 4 or bewlow is installed the following will process | |
303 | Remove-Item -Include $_.path -Force -Recurse -Verbose | |
304 | Write-Host "The recycling bin has been cleaned up successfully! " -NoNewline -ForegroundColor Green | |
305 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
306 | } | |
307 | } elseif ($PSVersionTable.PSVersion.Major -ge 5) { | |
308 | ## If PowerShell version 5 is running on the machine the following will process | |
309 | Clear-RecycleBin -DriveLetter C:\ -Force -Verbose | |
310 | Write-Host "The recycling bin has been cleaned up successfully! " -NoNewline -ForegroundColor Green | |
311 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
312 | } | |
313 | ||
314 | ## Starts cleanmgr.exe | |
315 | Function Start-CleanMGR { | |
316 | Try{ | |
317 | Write-Host "Windows Disk Cleanup is running. " -NoNewline -ForegroundColor Green | |
318 | Start-Process -FilePath Cleanmgr -ArgumentList '/sagerun:1' -Wait -Verbose | |
319 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
320 | } | |
321 | Catch [System.Exception]{ | |
322 | Write-host "cleanmgr is not installed! To use this portion of the script you must install the following windows features:" -ForegroundColor Red -NoNewline | |
323 | Write-host "[ERROR]" -ForegroundColor Red -BackgroundColor black | |
324 | } | |
325 | } Start-CleanMGR | |
326 | ||
327 | ## gathers disk usage after running the cleanup cmdlets. | |
328 | $After = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName, | |
329 | @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } }, | |
330 | @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f ( $_.Size / 1gb)}}, | |
331 | @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f ( $_.Freespace / 1gb ) } }, | |
332 | @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f ( $_.FreeSpace / $_.Size ) } } | | |
333 | Format-Table -AutoSize | Out-String | |
334 | ||
335 | ## Restarts wuauserv | |
336 | Get-Service -Name wuauserv | Start-Service -ErrorAction SilentlyContinue | |
337 | ||
338 | ## Stop timer | |
339 | $Enders = (Get-Date) | |
340 | ||
341 | ## Calculate amount of seconds your code takes to complete. | |
342 | Write-Verbose "Elapsed Time: $(($Enders - $Starters).totalseconds) seconds | |
343 | ||
344 | " | |
345 | ## Sends hostname to the console for ticketing purposes. | |
346 | Write-Host (Hostname) -ForegroundColor Green | |
347 | ||
348 | ## Sends the date and time to the console for ticketing purposes. | |
349 | Write-Host (Get-Date | Select-Object -ExpandProperty DateTime) -ForegroundColor Green | |
350 | ||
351 | ## Sends the disk usage before running the cleanup script to the console for ticketing purposes. | |
352 | Write-Verbose " | |
353 | Before: $Before" | |
354 | ||
355 | ## Sends the disk usage after running the cleanup script to the console for ticketing purposes. | |
356 | Write-Verbose "After: $After" | |
357 | ||
358 | ## Prompt to scan for large ISO, VHD, VHDX files. | |
359 | Function PromptforScan { | |
360 | param( | |
361 | $ScanPath, | |
362 | $title = (Write-Host "Search for large files" -ForegroundColor Green), | |
363 | $message = (Write-Host "Would you like to scan $ScanPath for ISOs or VHD(X) files?" -ForegroundColor Green) | |
364 | ) | |
365 | $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Scans $ScanPath for large files." | |
366 | $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Skips scanning $ScanPath for large files." | |
367 | $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no) | |
368 | $prompt = $host.ui.PromptForChoice($title, $message, $options, 0) | |
369 | switch ($prompt) { | |
370 | 0 { | |
371 | Write-Host "Scanning $ScanPath for any large .ISO and or .VHD\.VHDX files per the Administrators request." -ForegroundColor Green | |
372 | Write-Verbose ( Get-ChildItem -Path $ScanPath -Include *.iso, *.vhd, *.vhdx -Recurse -ErrorAction SilentlyContinue | | |
373 | Sort-Object Length -Descending | Select-Object Name, Directory, | |
374 | @{Name = "Size (GB)"; Expression = { "{0:N2}" -f ($_.Length / 1GB) }} | Format-Table | | |
375 | Out-String -verbose ) | |
376 | } | |
377 | 1 { | |
378 | Write-Host "The Administrator chose to not scan $ScanPath for large files." -ForegroundColor DarkYellow -Verbose | |
379 | } | |
380 | } | |
381 | } | |
382 | PromptforScan -ScanPath C:\ # end of function | |
383 | ||
384 | ## Completed Successfully! | |
385 | Write-Host (Stop-Transcript) -ForegroundColor Green | |
386 | ||
387 | Write-host " | |
388 | Script finished " -NoNewline -ForegroundColor Green | |
389 | Write-Host "[DONE]" -ForegroundColor Green -BackgroundColor Black | |
390 | ||
391 | } | |
392 | Start-Cleanup |