Controlling Folder Depth when Exporting Folder ACL to Excel via Powershell

  • The below script finds the permissions for the directory, but goes all the way through the entire file structure.

    $FolderPath = dir -Directory -Path "P:\Public" -Recurse -Force
    $Report = @()
    Foreach ($Folder in $FolderPath) {
        $Acl = Get-Acl -Path $Folder.FullName
        foreach ($Access in $acl.Access)
                $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD
    Group or
                $Report += New-Object -TypeName PSObject -Property $Properties
    $Report | Export-Csv -path "C:\export\permissions.csv"

    I need to go only two levels deep. I read that you can use Get-ChildItemToDepth but I am having trouble with the syntax if anyone can help.

  • Try this instead:

    $FolderPath = Get-ChildItem -Recurse -Depth 2 -Path "P:\Public" -Force

    Where -Depth is the how many levels deep you want to go.

    If you want to see what a cmdlet can do, you can use:

    Get-Help Get-ChildItem -Full