From 47c2666ddd162a511a6517d74502d3dc8465a430 Mon Sep 17 00:00:00 2001 From: Alex Hirsch Date: Sun, 6 Sep 2015 14:46:51 +0200 Subject: [PATCH] remove additional files inside Windows\WinSxS when removing onedrive --- lib/take-own.psm1 | 26 ++++++++++++++++++++++++++ scripts/remove-onedrive.ps1 | 11 +++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/take-own.psm1 b/lib/take-own.psm1 index 4540af7..6509188 100644 --- a/lib/take-own.psm1 +++ b/lib/take-own.psm1 @@ -32,6 +32,32 @@ function Takeown-Registry($key) { $key.SetAccessControl($acl) } +function Takeown-File($path) { + takeown.exe /A /F $path + $acl = Get-Acl $path + + # get administraor group + $admins = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544") + $admins = $admins.Translate([System.Security.Principal.NTAccount]) + + # add NT Authority\SYSTEM + $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($admins, "FullControl", "None", "None", "Allow") + $acl.AddAccessRule($rule) + + Set-Acl -Path $path -AclObject $acl +} + +function Takeown-Folder($path) { + Takeown-File $path + foreach ($item in Get-ChildItem $path) { + if (Test-Path $item -PathType Container) { + Takeown-Folder $item.FullName + } else { + Takeown-File $item.FullName + } + } +} + function Elevate-Privileges { param($Privilege) $Definition = @" diff --git a/scripts/remove-onedrive.ps1 b/scripts/remove-onedrive.ps1 index 4ea669b..27e7484 100644 --- a/scripts/remove-onedrive.ps1 +++ b/scripts/remove-onedrive.ps1 @@ -1,6 +1,8 @@ # Description: # This script will remove and disable OneDrive integration. +Import-Module -DisableNameChecking $PSScriptRoot\..\lib\take-own.psm1 + echo "Kill OneDrive process" taskkill.exe /F /IM "OneDrive.exe" taskkill.exe /F /IM "explorer.exe" @@ -36,3 +38,12 @@ rm -Force -ErrorAction SilentlyContinue "$env:userprofile\AppData\Roaming\Micros echo "Restarting explorer" start "explorer.exe" + +echo "Waiting for explorer to complete loading" +sleep 10 + +echo "Removing additional OneDrive leftovers" +foreach ($item in (ls "$env:WinDir\WinSxS\*onedrive*")) { + Takeown-Folder $item.FullName + rm -Recurse -Force $item.FullName +}