-
Notifications
You must be signed in to change notification settings - Fork 4
/
Clean-ReferenceOS.ps1
200 lines (165 loc) · 6.39 KB
/
Clean-ReferenceOS.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<#
.NOTES
===========================================================================
Originally Created by: SHISHIR KUSHAWAHA ([email protected])
changes by: Richard Tracy
Filename: Clean-ReferenceOS.ps1
Last Updated: 02/25/2019
Thanks to: unixuser011,W4RH4WK
===========================================================================
1. Run the script with Administrative access.
2. Put # chanracter Display-MessageCleanup delete() function if you want to skip any folder.
4. To add any new folder , need to declare the folder and call delete function with that folder variable
.DESCRIPTION
This script is created to automate the cleanup activity Display-MessageCleanup sysprep and capturing started. Doing so will reduce the size of .WIM file.
This script will perform the following
1. Clear windows temp and user temp folder
2. Empty recycle bin
3. Disk Cleanup
4. Clear CBS cabinet log files
5. Clear downloaded patches
6. Clear downloaded driver
7. Clean download folder
. PARAM
. LINKs
https://www.gngrninja.com/script-ninja/2016/5/24/powershell-calculating-folder-sizes
#>
##*===========================================================================
##* FUNCTIONS
##*===========================================================================
#region FUNCTION: Check if running in ISE
Function Test-IsISE {
# trycatch accounts for:
# Set-StrictMode -Version latest
try {
return ($null -ne $psISE);
}
catch {
return $false;
}
}
#endregion
#region FUNCTION: Check if running in Visual Studio Code
Function Test-VSCode{
if($env:TERM_PROGRAM -eq 'vscode') {
return $true;
}
Else{
return $false;
}
}
#endregion
#region FUNCTION: Find script path for either ISE or console
Function Get-ScriptPath {
<#
.SYNOPSIS
Finds the current script path even in ISE or VSC
.LINK
Test-VSCode
Test-IsISE
#>
param(
[switch]$Parent
)
Begin{}
Process{
if ($PSScriptRoot -eq "")
{
if (Test-IsISE)
{
$ScriptPath = $psISE.CurrentFile.FullPath
}
elseif(Test-VSCode){
$context = $psEditor.GetEditorContext()
$ScriptPath = $context.CurrentFile.Path
}Else{
$ScriptPath = (Get-location).Path
}
}
else
{
$ScriptPath = $PSCommandPath
}
}
End{
If($Parent){
Split-Path $ScriptPath -Parent
}Else{
$ScriptPath
}
}
}
##*===========================================================================
##* VARIABLES
##*===========================================================================
# Use function to get paths because Powershell ISE and other editors have differnt results
$scriptPath = Get-ScriptPath
[string]$scriptDirectory = Split-Path $scriptPath -Parent
[string]$scriptName = Split-Path $scriptPath -Leaf
[string]$scriptBaseName = [System.IO.Path]::GetFileNameWithoutExtension($scriptName)
$FunctionPath = Join-Path $scriptDirectory -ChildPath 'Functions'
##*========================================================================
##* Additional Runtime Function - REQUIRED
##*========================================================================
#Load functions from external files
. "$FunctionPath\Environment.ps1"
. "$FunctionPath\Cleanup.ps1"
. "$FunctionPath\Logging.ps1"
#build log name
[string]$FileName = $scriptBaseName +'.log'
#build global log fullpath
If(Test-SMSTSENV){
$Global:LogFilePath = Join-Path (Test-SMSTSENV -ReturnLogPath -Verbose) -ChildPath $FileName
}Else{
$RelativeLogPath = Join-Path -Path $scriptDirectory -ChildPath 'Logs'
}
Write-Host "Logging to file: $LogFilePath" -ForegroundColor Cyan
##*========================================================================
##* MAIN
##*========================================================================
[int]$OSBuildNumber = (Get-WmiObject -Class Win32_OperatingSystem).BuildNumber
#variables
$objShell = New-Object -ComObject Shell.Application
$Recyclebin = $objShell.Namespace(0xA)
$temp = (Get-ChildItem "env:\TEMP").Value
$WinTemp = "$env:SystemDrive\Windows\Temp\*"
$CBS="$env:SystemDrive\Windows\Logs\CBS\*"
$swtools="$env:SystemDrive\swtools\*"
$drivers="$env:SystemDrive\drivers\*"
$swsetup="$env:SystemDrive\swsetup\*"
$downloads="$env:SystemDrive\users\administrator\downloads\*"
$Prefetch="$env:SystemDrive\Windows\Prefetch\*"
$DowloadeUpdate="$env:SystemDrive\Windows\SoftwareDistribution\Download\*"
##*===========================================================================
##* MAIN
##*===========================================================================
# Remove temp files located in "C:\Users\USERNAME\AppData\Local\Temp"
Write-LogEntry "Emptying $temp..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent "$temp\*"
# Remove content of folder created during installation of driver
Write-LogEntry "Emptying $swtools..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent $swtools
# Remove content of folder created during installation of Lenovo driver
Write-LogEntry "Emptying $drivers..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent $drivers
# Remove content of folder created during installation of HP driver
Write-LogEntry "Emptying $swsetup..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent $swsetup
# Remove content of download folder of administrator account
Write-LogEntry "Emptying $downloads..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent $downloads
# Empty Recycle Bin
Write-LogEntry "Emptying Recycle Bin..." -Severity 1 -Source $scriptName -Outhost
$Recyclebin.items() | %{ Remove-FolderContent($_.path)}
# Remove Windows Temp Directory
Write-LogEntry "Emptying $WinTemp..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent $WinTemp
# Remove Prefetch folder content
Write-LogEntry "Emptying $Prefetch..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent $Prefetch
# Remove CBS log file
Write-LogEntry "Emptying $CBS..." -Severity 1 -Source $scriptName -Outhost
Remove-FolderContent $CBS
# Remove downloaded update
Remove-FolderContent $DowloadeUpdate
Initialize-DiskCleanupMgr -VolumeCache All