-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup-image.ps1
50 lines (36 loc) · 1.44 KB
/
cleanup-image.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
# cleanup-image.ps1
# Clean up a Windows image by removing temporary files and superseded updates
Set-StrictMode -Version 2
workflow Cleanup-Image {
parallel {
Cleanup-Profiles
# Cleanup WinSXS
Start-Process -Wait -File dism -ArgumentList "/online /Cleanup-Image /StartComponentCleanup /ResetBase"
# Empty Windows temp folders
Get-ChildItem $env:windir\temp,$env:temp | remove-item -recurse -force -ErrorAction Ignore
# Compact Windows Installer folder
Start-Process -Wait -File compact -ArgumentList "/C /EXE:LZX /S:$env:windir\Installer"
# Remove Appx packages for current user. They frequently prevent Sysprep from succeeding.
Get-AppxPackage | Remove-AppxPackage -ErrorAction Ignore
Cleanup-SoftwareDistribution
}
}
# Remove junk added to the user profiles by application installers.
function Cleanup-Profiles {
$profilePaths = @(
"$env:systemdrive\Users\*\Contacts\*",
"$env:systemdrive\Users\*\Desktop\*",
"$env:systemdrive\Users\*\Documents\*",
"$env:systemdrive\Users\*\Music\*",
"$env:systemdrive\Users\*\Pictures\*",
"$env:systemdrive\Users\*\Videos\*"
)
Get-ChildItem $profilePaths | remove-item -recurse -force
}
# Delete downloaded Windows Update files
function Cleanup-SoftwareDistribution {
Stop-Service "Wuauserv" -ErrorAction Ignore
Get-ChildItem $env:windir\SoftwareDistribution | remove-item -recurse -force -ErrorAction Ignore
}
Cleanup-Image
exit 0