Removing Invalid SID from folder permissions using PowerShell or icacls

Basil Shaikh 1 Reputation point
2022-11-12T17:57:05.137+00:00

I use the below code and tried with icacls too but didn't work at all, any idea please?

$path = "C:\TMP\Testing"
$acl = Get-Acl -Path $Path
foreach($acc in $acl.access )
{
$value = $acc.IdentityReference.Value
if($value -match "S-1-5-*")
{
$ACL.RemoveAccessRule($acc) | Out-Null
Set-Acl -Path $Path -AclObject $acl -ErrorAction Stop
Write-Host "Removed SID: $value from $Path "
}

Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-11-12T18:09:01.79+00:00

    Hi @Basil Shaikh ,

    maybe this helps to get started:

    https://www.alitajran.com/remove-orphaned-sids/
    http://www.ruudborst.nl/ps-one-liner-6-remove-orphaned-unresolvable-sids/

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2022-11-12T21:07:00.403+00:00

    You might be trying to remove an inherited rule.

    $path = "c:\tmp\testing"  
    $acl = Get-Acl -Path $Path  
    foreach($acc in $acl.access )  
    {  
        $value = $acc.IdentityReference.Value  
        #$value  
       
        if($value -match "S-1-5-*")  
        {   
            if ($acc.IsInherited)   
            {  
                "Skipping inherited Access rule: $value"  
            }   
            else    
            {  
                $ACL.RemoveAccessRule($acc) | Out-Null  
                Set-Acl -Path $Path -AclObject $acl -ErrorAction Stop  
                Write-Host "Removed SID: $value from $Path "  
            }  
        }  
    }  
    

    You should also be aware that some dead SID's aren't dead. They are Capability SIDs.

    https://www.bleepingcomputer.com/news/microsoft/windows-10-could-break-if-capability-sids-are-removed-from-permissions/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.