PowerShell: Delete Hidden System Volume Information Directory

# Incident:
The System Volume Information has been copied over to an SMB share, where such destination would have no use of the extra metadata. Deleting that special directory requires mucho badabing badabang as shown herein!

# Problem:
PS C:\temp> rm -force $folderToDelete\*
rm : Cannot remove item \\FILESHERVER007\SHARE999\System Volume Information\
Information\{099d1ec4-2ba1-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc752}: Access to the path is denied.
At line:1 char:1
+ rm -force $folderToDelete\*
# Solution:

$folderToDelete="\\FILESHERVER007\SHARE999\System Volume Information"

function seizeOwnership {
param(
[String]$folder
)
# Take Ownership of the folder
takeown.exe /A /F $folder

# Grant other Administrators access to such folder
$CurrentACL = Get-Acl $folder

$systemACLPermission = "NT AUTHORITY\SYSTEM","FullControl","ContainerInherit,ObjectInherit","None","Allow"
$SystemAccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $SystemACLPermission
$CurrentACL.AddAccessRule($SystemAccessRule)
write-host "NT Authority\SYSTEM is added" -Fore Yellow

$localAdminACLPermission = "$env:computername\Administrators","FullControl","ContainerInherit,ObjectInherit","None","Allow"
$SystemAccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $localAdminACLPermission
$CurrentACL.AddAccessRule($SystemAccessRule)
write-host "$env:computername\Administrators added" -Fore Yellow

$domainAdminACLPermission = "$env:USERDNSDOMAIN\Domain Admins","FullControl","ContainerInherit,ObjectInherit","None","Allow"
$SystemAccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $domainAdminACLPermission
$CurrentACL.AddAccessRule($SystemAccessRule)
write-host "Domain Admins are added" -Fore Yellow

Set-Acl -Path $folder -AclObject $CurrentACL
}

$filesToDelete=gci $folderToDelete -force
seizeOwnership -folder $folderToDelete
$x=gi $folderToDelete -force
$x.Attributes=''
$emptyDirectory="C:\emptyFolder"
mkdir $emptyDirectory
rm -force $emptyDirectory\*
robocopy $emptyDirectory $folderToDelete /purge
rm -force $folderToDelete
# Result
PS C:\temp> robocopy $emptyDirectory $folderToDelete /purge

-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------

Started : Sunday, January 5, 2020 1:50:58 AM
Source : C:\emptyFolder\
Dest : \\FILESHERVER007\SHARE999\System Volume Information\

Files : *.*

Options : *.* /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30

------------------------------------------------------------------------------

0 C:\emptyFolder\
*EXTRA File 16.2 m {099d1ec4-2ba1-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {099d20f2-2ba1-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {099d2ea0-2ba1-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.2 m {0ba84fd0-54f2-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.3 m {0ba854e4-54f2-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 53.5 m {13ab3acd-fa68-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 105.3 m {1423b1c6-b7b8-11e9-9953-0026b9f9021c}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 175.6 m {318c79b4-992a-11e9-9953-0026b9f9021c}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.1 m {32e85e1b-2a98-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 17.7 m {32e85eae-2a98-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 65536 {3808876b-c176-4e48-b7ae-04046e6cc752}
*EXTRA File 9.2 m {3c8816c5-53d0-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 17.8 m {3c881ba4-53d0-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.3 m {3c881e78-53d0-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.2 m {3c882135-53d0-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.3 m {3c882436-53d0-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.2 m {3c8828d9-53d0-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 17.8 m {5ade4d52-5889-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 17.9 m {6920789d-54a1-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.2 m {69207c0e-54a1-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.3 m {69207eb6-54a1-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.2 m {877a6920-5484-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 25.0 m {93af58f8-5956-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {94ed3d19-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {94ed3ea8-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {94ed3ecf-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {94ed3f31-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {94ed3f69-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {94ed3fbe-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 1.9 g {94ed400b-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 1.9 g {94ed4021-2f59-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 31.5 m {9e342180-5ad0-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 17.9 m {a27b45e8-57b8-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.2 m {b310937f-553c-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 7.5 m {b84aff7c-2d98-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.1 m {b84b1352-2d98-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 30.0 m {b84b16e9-2d98-11ea-93f4-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 6.0 m {bf94c291-53ae-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.3 m {cb918793-554e-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 31.7 m {cb918c03-554e-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}
*EXTRA File 9.3 m {f541be15-5472-11e8-b3e5-0026b9f8f878}{3808876b-c176-4e48-b7ae-04046e6cc
752}

------------------------------------------------------------------------------

Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 0 1 0 0 0
Files : 0 0 0 0 0 41
Bytes : 0 0 0 0 0 4.691 g
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : Sunday, January 5, 2020 1:50:59 AM

Leave a Reply

Your email address will not be published. Required fields are marked *