PowerShell - Get Folder Permissions by UNC Recursive
- Jon Boyette
- Jan 14, 2022
- 1 min read
Save as another Permission Walk.ps1 or named .ps1, This one is fast and informative including: Identity, AccessType, Permission, Inherited, Inheritance, BlockInheritance for parent and child output to csv
#############################
$TargetDirectory = "\\SERVER\SHARE" # UNC to Share
#############################
Function SubDirs {
Param (
$fDir
)
Return @(Get-ChildItem -Directory -Path $fDir | %{$_.FullName})
}
$OutFile = ($TargetDirectory.Substring(2)).Replace("\","_") + "_PermissionsWalk.csv"
If (Get-Item ".\$($OutFile)" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue) {Remove-Item ".\$($OutFile)" -Confirm:$false -Force}
$RootFolder = Get-Item -Path $TargetDirectory
Write-Host $RootFolder.FullName
Get-Acl $RootFolder.FullName | %{
$BlockInheritance = $_.AreAccessRulesProtected
$_.Access | %{
New-Object -TypeName PSObject -Property @{
Path = $RootFolder.FullName
Identity = $_.IdentityReference
AccessType = $_.AccessControlType
Permission = $_.FileSystemRights
Inherited = $_.IsInherited
Inheritance = $_.InheritanceFlags
BlockInheritance = $BlockInheritance
} | Select Path,Identity,AccessType,Permission,Inherited,Inheritance,BlockInheritance | Export-Csv $OutFile -NoTypeInformation -Append
}
}
$DirArr = Get-ChildItem -Directory -Path $TargetDirectory | %{$_.FullName}
For ($i=0; $i -lt $DirArr.Count; $i++) {
$DirObj = Get-Item -Path $DirArr[$i]
Write-Host $DirObj.FullName
Get-Acl $DirObj.FullName | %{
$BlockInheritance = $_.AreAccessRulesProtected
$_.Access | %{
New-Object -TypeName PSObject -Property @{
Path = $DirArr[$i]
Identity = $_.IdentityReference
AccessType = $_.AccessControlType
Permission = $_.FileSystemRights
Inherited = $_.IsInherited
Inheritance = $_.InheritanceFlags
BlockInheritance = $BlockInheritance
} | Select Path,Identity,AccessType,Permission,Inherited,Inheritance,BlockInheritance | Export-Csv $OutFile -NoTypeInformation -Append
}
}
$DirArr += SubDirs $DirArr[$i]
}
Comments