-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-kb.ps1
executable file
·197 lines (184 loc) · 7.87 KB
/
remove-kb.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
$kbIDs=(
"KB2902907",
"KB2952664", #Compatibility test for Win7
"KB2976978", #telemetry for Win8/8.1
"KB2990214", #Get Windows 10 for Win7 without sp1
"KB2952664", #Get Windows 10 assistant
"KB3012973", #Windows 10 suggested (Forced?) update
"KB3021917", #telemetry for Win7
"KB3022345", #telemetry
"KB3035583", #Get Windows 10 for Win7sp1/8.1
"KB3044374", #Get Windows 10 for Win8.1
"KB3050265", #Update for Windows Update on Win7
"KB3065987", #Update for Windows Update on Win7/Server 2008R2
"KB3068708", #telemetry
"KB3075249", #telemetry for Win7/8.1
"KB3075851", #Update for Windows Update on Win7
"KB3075853", #Update on Win8.1/Server 2012R2
"KB3080149", #telemetry for Win7/8.1
"KB3081437", #Windows 10 compatibility update
"KB3081454", #Windows 10 compatibility update
"KB3083324", #Windows 7 to Windows 10 preparation update
"KB3083325", #Windows 8 to Windows 10 preparation update
"KB3150513", #Windows 10 compatibility modile for Win7/Win8 (may 2016)
"KB3112336", #Windows 8.1 > Windows 10
"KB3112343" #Windows 7 - 8.1 > Windows 10
)
$sheduledTasks=(
@{name = "launchtrayprocess"; directory = "\Microsoft\Windows\Setup\GWX"},
@{name = "refreshgwxconfig"; directory = "\Microsoft\Windows\Setup\GWX"},
@{name = "refreshgwxconfigandcontent"; directory = "\Microsoft\Windows\Setup\GWX"},
@{name = "refreshgwxcontent"; directory = "\Microsoft\Windows\Setup\GWX"}
)
$gwx_dirs=(
"$env:windir\system32\GWX",
"$env:windir\SysWOW64\GWX",
"C:\`$WINDOWS.~BT"
)
# You need to modify this variable whith administrator group name
# Here for French Windows this is Administrateurs
$adminGroup="Administrateurs"
$yes="O"
function add_reg_object {
param (
$reg_path,
$reg_name,
$reg_value,
$value_type
)
if (!(Test-Path $reg_path)){
Write-Host -nonewline "creating $reg_path ..."
New-Item -Path $reg_path -Force | Out-Null
}
if ($reg_name -and $reg_value){
Write-Host "Reg key $reg_name created with value $reg_value"
if ($value_type){
New-ItemProperty -Path $reg_path -Name $reg_name -Value $reg_value -PropertyType $value_type -Force | Out-Null
}
else {
New-ItemProperty -Path $reg_path -Name $reg_name -Value $reg_value -Force | Out-Null
}
}
else {
Write-Host "[ERROR] add_reg_value : no `$reg_name or `$reg_value parameters..."
}
}
function remove_tasks () {
param($taskList)
Foreach ($task in $taskList){
Write-Host -ForegroundColor white -NoNewline "Remove Task " $task.name
if ($PSVersionTable.PSVersion.Major -gt 2) {
if (Get-ScheduledTask -TaskName $task.name -ErrorAction SilentlyContinue) {
Write-Host -NoNewline -ForegroundColor DarkGreen " found! "
Write-Host -Nonewline -ForegroundColor white "removing ... "
Try {Unregister-ScheduledTask -TaskName $task.name -ErrorAction SilentlyContinue -Confirm:$false}
Catch {
Write-Host -Nonewline -ForegroundColor white " Error "
}
Write-Host -ForegroundColor Green " Done"
}
else { Write-Host -ForegroundColor Yellow " Already removed"}
}
else {
$currentTask = $task.directory + "\" + $task.name
if(schtasks /Query /TN $currentTask 2>$null) {
Write-Host -NoNewline -ForegroundColor DarkGreen " found! "
Write-Host -Nonewline -ForegroundColor white "removing ... "
try{
echo $yes | schtasks /Delete /TN $currentTask /F 2>$null
}
Catch {
Write-Host -Nonewline -ForegroundColor white " Error "
}
Write-Host -ForegroundColor green "Done"
}
else { Write-Host -ForegroundColor Yellow " Already removed" }
}
}
}
function hide_update() {
param($kbList)
Write-Host -ForegroundColor Gray "Creating updated database, this take 5 to 10 minutes.`nPlease wait ...`n"
$session = New-Object -ComObject "Microsoft.Update.Session"
$searcher = $session.CreateUpdateSearcher()
$searcher.Online = $true
$searcher.IncludePotentiallySupersededUpdates = $true
$criteria = "IsInstalled=0"
$result = $searcher.Search($criteria)
Foreach ($kb in $kbList){
Write-Host -NoNewline -ForegroundColor White "Hide $kb : "
$id = $kb.Replace("KB","")
$found = 0
Foreach ($update in $result.Updates) {
if ($update.KBArticleIDs -eq $id) {
#Some updates have ... updates
if ($found) {
Write-Host -NoNewline -ForegroundColor Gray " └ $kb : "
}
if (!$update.IsHidden) {
$update.IsHidden = "True"
Write-Host -ForegroundColor green "Hidden"
}
else {
Write-Host -ForegroundColor Yellow "Already hidden"
}
$found = 1
}
}
if (!$found){ Write-Host -ForegroundColor Red "Not found" }
}
}
#Remove GWX Files (test)
function lock_dir {
param([string]$dir)
Write-Host -ForegroundColor white "removing $dir content ... "
takeown /F "$dir" /R /D $yes 2>&1 | Out-Null
icacls "$dir" /C /grant $adminGroup":F" /T 2>&1 | Out-Null
Try{ Remove-Item $dir\* -Force -Recurse -ErrorAction SilentlyContinue}
Catch {Write-Host -ForegroundColor Red "Some files can't be deleted."}
#lock GWX directory
icacls "$dir" /deny *S-1-1-0:`(CI`)`(OI`)F 2>&1 | Out-Null
}
Write-Host -Nonewline -ForegroundColor white "Searching for GWX process ... "
if (Get-Process -name GWX -ErrorAction SilentlyContinue) {
Write-Host -ForegroundColor DarkGreen -NoNewLine "Running "
Write-Host -ForegroundColor white "removing ... "
Try {Stop-Process -name GWX -Force -ErrorAction SilentlyContinue}
Catch { Write-Host -ForegroundColor Red "Error"}
}
else { Write-Host -ForegroundColor Yellow ("Not running")}
Write-Host -ForegroundColor white "`nRemoving and locking GWX folders...`n--------------------------------- "
$gwx_dirs | ForEach {
lock_dir $_
}
Write-Host -ForegroundColor white "`nRemoving Updates...`n------------------- "
Foreach($kbID in $kbIDs){
$kbNum = $kbID.Replace("KB","")
Write-Host -NoNewline -ForegroundColor white "Uninstalling $kbID : "
if (Get-HotFix -Id $kbID -ErrorAction SilentlyContinue){
Write-Host -NoNewline -ForegroundColor DarkGreen "found! "
Write-Host -Nonewline -ForegroundColor white "removing ... "
wusa.exe /uninstall /KB:$kbNum /norestart /quiet
Do
{
Start-Sleep -Seconds 3
}while(Invoke-Command -ScriptBlock {Get-Process | Where-Object {$_.name -eq "wusa"}})
if(Get-HotFix -Id $kbID -ErrorAction SilentlyContinue){
Write-Host -ForegroundColor Red "Failed"
}
else{
Write-Host -ForegroundColor Green "Done"
}
}
else {
Write-Host -ForegroundColor Yellow ("Not installed")
}
}
Write-Host -ForegroundColor white "`nHiding Updates...`n---------------- "
hide_update $kbIDs
Write-Host -ForegroundColor white "`nRemoving sheduled tasks...`n--------------------------"
remove_tasks $sheduledTasks
Write-Host -ForegroundColor white "`nUpdate Registrery to prevent Win10 automatic installation...`n------------------------------------------------------------"
add_reg_object "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\OSUpgrade" "AllowOSUpgrade" "0" "DWord"
add_reg_object "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" "DisableOSUpgrade" "1" "DWord"
add_reg_object "HKLM:\SOFTWARE\Policies\Microsoft\Windows\GWX" "DisableGWX" "1" "DWord"