TONSCHUH

Package & Telemetry Removal Guide v1.0

Aug 13th, 2016
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.09 KB | None | 0 0
  1. http://forums.mydigitallife.info/threads/70798-Package-amp-Telemetry-Removal-Guide-v1-0?p=1264782&viewfull=1#post1264782
  2.  
  3. Package & Telemetry Removal Guide v1.0
  4.  
  5. Hi All!
  6.  
  7. In this guide I’m going to try to help you & give the best tools for removal of unwanted Windows 10
  8. Components, (Up-to-date with Anniversary edition 1607).
  9.  
  10. First, let me explain my philosophy, I don’t like to remove components from the system unless
  11. It’s really necessary. Most things can be achieved by disabling instead.
  12. Then you always have the option to roll-back if something unforeseen happens.
  13. Also, making a “real” backup is also crucial, especially if you remove system apps.
  14. With a real backup I mean making a diskimage. Acronis is a good program for this.
  15. Before running any of the tools below, make sure you disable Windows Uppdate & System Restore.
  16. (These can be re-enabled after at least 1 re-boot, when you are done with the tools).
  17.  
  18. Which app or package to remove is out of the scope of this article, there are plenty of lists around,
  19. And opinions vary.what I want to do is give you the best way of doing it without screwing your system.
  20. What app types are there?
  21. Well, not to go to deep there are basically 2.
  22. 1. Provisioned apps, these are the ones that get installed by default in any new user profile.
  23. 2. System apps, these are globally installed for everyone.
  24.  
  25. To uninstall we use Powershell.
  26. In order to uninstall a provisioned app for all users, you must uninstall the provisioned app package. Example:
  27. Get-AppXProvisionedPackage -Online | where DisplayName -EQ Microsoft.WindowsCalculator | Remove-AppxProvisionedPackage –Online
  28. To uninstall it from your local profile you use:
  29. Get-AppxPackage -Name Microsoft.WindowsCalculator -AllUsers | Remove-AppxPackage
  30.  
  31. Now, removing system apps is another ballgame.
  32. These can only be removed with Dism.exe and some trickery.
  33. System apps & their reg keys can only be removed by the system account “Trustedinstaller”, as it's the owner, Administrators are locked out.
  34. There are 2 approaches to achieve that:
  35. 1. Impersonate Trustedinstaller with a special exe file that uses it’s token.
  36. 2. Take ownership of the package registry keys
  37.  
  38. There is a tool called install_wim_tweak.exe for removing system apps, but I don’t like that one.
  39. The reason is because it removes all Owner subkeys of apps under the branch:
  40. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages\
  41. even if you just use it to make a list of packages!
  42. (There are almost 3000 of them)
  43. That can potentially cause dependency issues, therefore I don’t recommend it.
  44. Better is to use CBSEnum_x64.exe with it’s gui, or Powershell scripts that specifically target the package we are interested in, instead of “lawn mowing” like Wim Tweak does. Also, both these techniques uses Windows own Dism.exe in the removal process.
  45.  
  46. Ok, let’s get started:
  47. First tool is CBSEnum_x64.exe which can be downloaded from here:
  48.  
  49. https://bitbucket.org/himselfv/cbsenum
  50.  
  51. This uses the “Trustedinstaller trick” so go and get the RunAsTI64.exe here:
  52.  
  53. https://github.com/jschicht/RunAsTI
  54.  
  55. https://bitbucket.org/himselfv/cbsenum/raw/tip/Docs/cbsenum-0.8-screen.png
  56.  
  57. To impersonate Trustedinstaller copy the RunAsTI64.exe & CBSEnum_x64.exe to the root of C:
  58. (Add exclusion to RunAsTI64.exe in antivirus, as some don't like it)
  59. Doubleclick RunAsTI64.exe, at command prompt type cd\ and enter, then type CBSEnum_x64 and enter again.(It opens)
  60. First thing to do is create a list that you can search. Under File “Save package list” to some location.
  61. Search for the full or partial package name.
  62. Once found, go to CBSenum and choose viewing by “Flat list”.
  63. Scroll down until you find the package and choose Visibility=>Make >Visible, Then Manage=>Decouple.
  64. Now open Powershell as Admin (Not Windows PowerShell (x86).
  65. Go back to CBSenum and rightclick package and choose Copy=>Uninstallation commands
  66. Paste in Powershell, done!
  67. Do not try to use the uninstall option in CBSEnum, it wont work.
  68. Do NOT use any of the options under the EDIT meny.
  69. If you want to remove several packages without restarting, add the /NoRestart switch to the end of the Uninstallation commands.
  70.  
  71. ========================================================================
  72.  
  73. Now for the Powershell script solution,(That uses the Take ownership route).
  74. Credit to W4RH4WK where you can also find more scripts here:
  75.  
  76. https://github.com/W4RH4WK/Debloat-Windows-10
  77.  
  78. This is also very good, as it only goes after the target package and leave everything else intact.
  79. Especially when removing several packages, and it also accept wildcards, ok here we go:
  80.  
  81. 1. Create a folder named Debloat in the root of C: (C:\Debloat)
  82. 2. Inside this folder create 2 more folders named lib and scripts
  83. 3. Inside the lib folder drop the take-own.psm1 file (All files below in spoiler tags)
  84. 4. Inside the scripts folder drop the systemappremove.ps1 & provisionappremove.ps1 files
  85. 5. Open Powershell as Admin and type Set-ExecutionPolicy Unrestricted say “Yes to all”.
  86. 6. Then type cd\ and enter , then type cd Debloat and enter, now inside the Debloat folder
  87. Type ls -Recurse *.ps1 | Unblock-File and enter & ls -Recurse *.psm1 | Unblock-File and enter.
  88. Now you can run the scripts!, to remove systemapps type scripts\systemappremove and enter, and so on.
  89. Inside the scripts you see that there are many apps mentioned with a hashtag in front (#)
  90. That hashtag tells Powershell not to run the line, so if you don’t want it removed, keep the hashtag in front of the app name.
  91. In script below "ContentDeliveryManager" & "AllowTelemetry" Packages will be removed.
  92.  
  93. To get a list of all Provisioned apps, type in the following in Powershell:
  94. Get-AppXPackage -AllUsers | Select Name > C:\packages.txt
  95.  
  96. Here are the files:
  97.  
  98. take-own.psm1 (Inside lib folder)
  99.  
  100. =======================================================================================================================================
  101. function Takeown-Registry($key) {
  102. # TODO does not work for all root keys yet
  103. switch ($key.split('\')[0]) {
  104. "HKEY_CLASSES_ROOT" {
  105. $reg = [Microsoft.Win32.Registry]::ClassesRoot
  106. $key = $key.substring(18)
  107. }
  108. "HKEY_CURRENT_USER" {
  109. $reg = [Microsoft.Win32.Registry]::CurrentUser
  110. $key = $key.substring(18)
  111. }
  112. "HKEY_LOCAL_MACHINE" {
  113. $reg = [Microsoft.Win32.Registry]::LocalMachine
  114. $key = $key.substring(19)
  115. }
  116. }
  117.  
  118. # get administraor group
  119. $admins = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
  120. $admins = $admins.Translate([System.Security.Principal.NTAccount])
  121.  
  122. # set owner
  123. $key = $reg.OpenSubKey($key, "ReadWriteSubTree", "TakeOwnership")
  124. $acl = $key.GetAccessControl()
  125. $acl.SetOwner($admins)
  126. $key.SetAccessControl($acl)
  127.  
  128. # set FullControl
  129. $acl = $key.GetAccessControl()
  130. $rule = New-Object System.Security.AccessControl.RegistryAccessRule($admins, "FullControl", "Allow")
  131. $acl.SetAccessRule($rule)
  132. $key.SetAccessControl($acl)
  133. }
  134.  
  135. function Takeown-File($path) {
  136. takeown.exe /A /F $path
  137. $acl = Get-Acl $path
  138.  
  139. # get administraor group
  140. $admins = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
  141. $admins = $admins.Translate([System.Security.Principal.NTAccount])
  142.  
  143. # add NT Authority\SYSTEM
  144. $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($admins, "FullControl", "None", "None", "Allow")
  145. $acl.AddAccessRule($rule)
  146.  
  147. Set-Acl -Path $path -AclObject $acl
  148. }
  149.  
  150. function Takeown-Folder($path) {
  151. Takeown-File $path
  152. foreach ($item in Get-ChildItem $path) {
  153. if (Test-Path $item -PathType Container) {
  154. Takeown-Folder $item.FullName
  155. } else {
  156. Takeown-File $item.FullName
  157. }
  158. }
  159. }
  160.  
  161. function Elevate-Privileges {
  162. param($Privilege)
  163. $Definition = @"
  164. using System;
  165. using System.Runtime.InteropServices;
  166.  
  167. public class AdjPriv {
  168. [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  169. internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr rele);
  170.  
  171. [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  172. internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  173.  
  174. [DllImport("advapi32.dll", SetLastError = true)]
  175. internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  176.  
  177. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  178. internal struct TokPriv1Luid {
  179. public int Count;
  180. public long Luid;
  181. public int Attr;
  182. }
  183.  
  184. internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  185. internal const int TOKEN_QUERY = 0x00000008;
  186. internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  187.  
  188. public static bool EnablePrivilege(long processHandle, string privilege) {
  189. bool retVal;
  190. TokPriv1Luid tp;
  191. IntPtr hproc = new IntPtr(processHandle);
  192. IntPtr htok = IntPtr.Zero;
  193. retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
  194. tp.Count = 1;
  195. tp.Luid = 0;
  196. tp.Attr = SE_PRIVILEGE_ENABLED;
  197. retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
  198. retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
  199. return retVal;
  200. }
  201. }
  202. "@
  203. $ProcessHandle = (Get-Process -id $pid).Handle
  204. $type = Add-Type $definition -PassThru
  205. $type[0]::EnablePrivilege($processHandle, $Privilege)
  206. }
  207. =======================================================================================================================================
  208.  
  209. systemappremove.ps1 (Inside scripts folder)
  210.  
  211. =======================================================================================================================================
  212. Import-Module -DisableNameChecking $PSScriptRoot\..\lib\take-own.psm1
  213.  
  214. echo "Elevating priviledges for this process"
  215. do {} until (Elevate-Privileges SeTakeOwnershipPrivilege)
  216.  
  217. echo "Force removing system apps"
  218. $needles = @(
  219. #"Anytime"
  220. #"BioEnrollment"
  221. #"Browser"
  222. #"ContactSupport"
  223. #"Cortana" # This will disable startmenu search.
  224. #"Defender"
  225. "ContentDeliveryManager"
  226. "AllowTelemetry"
  227. #"Gaming"
  228. #"InternetExplorer"
  229. #"Maps"
  230. #"OneDrive"
  231. #"Wallet"
  232. #"Xbox"
  233. )
  234.  
  235. foreach ($needle in $needles) {
  236. echo "Trying to remove all packages containing $needle"
  237.  
  238. $pkgs = (ls "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages" |
  239. where Name -Like "*$needle*")
  240.  
  241. foreach ($pkg in $pkgs) {
  242. $pkgname = $pkg.Name.split('\')[-1]
  243.  
  244. Takeown-Registry($pkg.Name)
  245. Takeown-Registry($pkg.Name + "\Owners")
  246.  
  247. Set-ItemProperty -Path ("HKLM:" + $pkg.Name.Substring(18)) -Name Visibility -Value 1
  248. New-ItemProperty -Path ("HKLM:" + $pkg.Name.Substring(18)) -Name DefVis -PropertyType DWord -Value 2
  249. Remove-Item -Path ("HKLM:" + $pkg.Name.Substring(18) + "\Owners")
  250.  
  251. dism.exe /Online /Remove-Package /PackageName:$pkgname /NoRestart
  252. }
  253. }
  254. =======================================================================================================================================
  255.  
  256. provisionappremove.ps1 (Inside scripts folder)
  257.  
  258. =======================================================================================================================================
  259. # Description:
  260. # This script removes unwanted Apps that come with Windows. If you do not want
  261. # to remove certain Apps comment out the corresponding lines below.
  262.  
  263. Import-Module -DisableNameChecking $PSScriptRoot\..\lib\take-own.psm1
  264.  
  265. echo "Elevating priviledges for this process"
  266. do {} until (Elevate-Privileges SeTakeOwnershipPrivilege)
  267.  
  268. echo "Uninstalling default apps"
  269. $apps = @(
  270. # default Windows 10 apps
  271. "Microsoft.3DBuilder"
  272. "Microsoft.Appconnector"
  273. "Microsoft.BingFinance"
  274. "Microsoft.BingNews"
  275. "Microsoft.BingSports"
  276. "Microsoft.BingWeather"
  277. #"Microsoft.FreshPaint"
  278. "Microsoft.Getstarted"
  279. "Microsoft.MicrosoftOfficeHub"
  280. #"Microsoft.MicrosoftSolitaireCollection"
  281. #"Microsoft.MicrosoftStickyNotes"
  282. "Microsoft.Office.OneNote"
  283. #"Microsoft.OneConnect"
  284. "Microsoft.People"
  285. "Microsoft.SkypeApp"
  286. #"Microsoft.Windows.Photos"
  287. #"Microsoft.WindowsAlarms"
  288. #"Microsoft.WindowsCalculator"
  289. "Microsoft.WindowsCamera"
  290. "Microsoft.WindowsMaps"
  291. "Microsoft.WindowsPhone"
  292. #"Microsoft.WindowsSoundRecorder"
  293. #"Microsoft.WindowsStore"
  294. "Microsoft.XboxApp"
  295. "Microsoft.ZuneMusic"
  296. "Microsoft.ZuneVideo"
  297. "microsoft.windowscommunicationsapps"
  298. "Microsoft.MinecraftUWP"
  299.  
  300. # Threshold 2 apps
  301. "Microsoft.CommsPhone"
  302. "Microsoft.ConnectivityStore"
  303. "Microsoft.Messaging"
  304. "Microsoft.Office.Sway"
  305.  
  306. # non-Microsoft
  307. "9E2F88E3.Twitter"
  308. "Flipboard.Flipboard"
  309. "ShazamEntertainmentLtd.Shazam"
  310. "king.com.CandyCrushSaga"
  311. "king.com.CandyCrushSodaSaga"
  312. "king.com.*"
  313. "ClearChannelRadioDigital.iHeartRadio"
  314. "TheNewYorkTimes.NYTCrossword"
  315.  
  316. # apps which cannot be removed using Remove-AppxPackage
  317. #"Microsoft.BioEnrollment"
  318. #"Microsoft.MicrosoftEdge"
  319. #"Microsoft.Windows.Cortana"
  320. #"Microsoft.WindowsFeedback"
  321. #"Microsoft.XboxGameCallableUI"
  322. #"Microsoft.XboxIdentityProvider"
  323. #"Windows.ContactSupport"
  324. )
  325.  
  326. foreach ($app in $apps) {
  327. echo "Trying to remove $app"
  328.  
  329. Get-AppxPackage -Name $app -AllUsers | Remove-AppxPackage
  330.  
  331. Get-AppXProvisionedPackage -Online |
  332. where DisplayName -EQ $app |
  333. Remove-AppxProvisionedPackage -Online
  334. }
  335. =======================================================================================================================================
  336.  
  337. That's it folks !
Add Comment
Please, Sign In to add comment