1

I'm new to Powershell and I need to know how to list all permissions of a folder for a specific user. This is what I have discovered so far:

$ReferenceAccountName = 'DOMAIN\Username' 
[string[]]$SearchDirectories
= @('X:\SomeDirectory', 'F:\AnotherDirectory')

foreach ($RootDir in $SearchDirectories) {
    $DirACL = Get-Acl -Path $RootDir
    foreach ($ACL in $DirACL.Access){
        if ($ACL.IdentityReference -like $ReferenceAccountName){
            Write-Output $RootDir
        }
    }
    foreach ($Directory in (Get-ChildItem -Path $RootDir -Recurse | `
                            Where-Object -FilterScript {$_.Attributes `
                            -contains 'Directory'})){
        $DirACL = Get-Acl -Path $Directory.FullName
        foreach ($ACL in $DirACL.Access){
            if ($ACL.IdentityReference -like $ReferenceAccountName){
                Write-Output $Directory.FullName
            }
        }
    } }

However, this does not return any value with the Write-Output command. Changing it to Write-Host didn't work as well. Am I missing some crucial parts?

I'm using Powershell V2.0

verfluecht
  • 493
  • 6
  • 24
Andr0mega
  • 65
  • 3
  • 13

1 Answers1

1

If I understood your question correctly, this should make it:

$user = "domain\username"
$path = "C:\Users\daisies"
$list = Get-ChildItem $path -Recurse | Where-Object {(Get-Acl $_.FullName).Access | Where-Object {$_.IdentityReference -eq $user} }
Write-Output $list

This outputs a list with your permission, LastWriteTime and filename.

verfluecht
  • 493
  • 6
  • 24
  • This did indeed work. Thanks a lot. Would it also be able to exclude inherited permissions? – Andr0mega Apr 17 '19 at 06:22
  • Hmm. It still has somer errors. The folders won't update if I change the permissions and rerun the command. Any idea why? – Andr0mega Apr 17 '19 at 10:23
  • @Andr0mega I guess this a caching issue. The Information is cached and since you are not renewing the Variables, PS does not change anything. As you can see [here](https://stackoverflow.com/questions/14502611/does-powershell-have-a-cache-that-needs-to-be-cleared), you will need to add `Remove-Variable varname` – verfluecht Apr 18 '19 at 05:14