Search code examples
powershelldirectoryaclsubdirectoryget-childitem

Powershell get-acl from childitems that not equals foldername


At work we have a folder with lots of subfolders named like "MeyerS". (Lastname and the first letter of surname)

When I take a look at Get-ChildItem $path | Get-Acl the username equals the subfolder-name. But there is also a "SCHUELER\" in front of "MeyerS". This is what the output looks like a.e.: SCHUELER\MeyerS Allow Write, ReadAndExecute, Synchronize

Some subfolders don't have this kind of username. Now I want to output all these subfolders without this username- "combination". With my first codesnippet I get all of them, but I really just want these specific ones.

I checked some similar questions, and found something. I modified it, but it shows all subfolders just without SCHUELER\MeyerS. I think I just need a small push to the right way.

The code so far:

 $path = "R:\HOME"
 $folders = Get-ChildItem $path | where {$_.psiscontainer}

 foreach ($folder in $folders){

      $domain = "domname"
      $aclname = "ACLname"
      $aclfullname ="$domain\$aclname"

      Get-Acl | select -ExpandProperty Access | where {$_.identityreference -notcontains $aclfullname}

      Write-Host $folder.FullName}

Short note: I tried a lot of variations with -noteq or -notlike.
What do I have to change?

If there is already an answer I really didn't know.
Sometimes it's really hard to enunciate yourself in another language. I hope you get my point.

Thanks.


Solution

  • $path = "R:\HOME"
    $folders = Get-ChildItem $path | where {$_.psiscontainer}
    
    foreach ($folder in $folders)
    {
    
        $domain = "domname"
        $aclname = "ACLname"
        $aclfullname ="$domain\$aclname"
        $FoldersWithAclFullName = $null
    
    
        $FoldersWithAclFullName = Get-Acl -Path $Folder `
        | Select-Object -ExpandProperty Access `
        | Where-Object -Property IdentityReference -ne -Value $aclfullname
    
        if ( -not $FoldersWithAclFullName )
        {
            Write-Host $folder.FullName
        }
    }