Advertisement
joedigital

ps-get non-inherited ACLs with dotnet

Jun 23rd, 2025 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Retrieves the non-inherited Access Control List (ACL) for a specified folder using .NET methods.
  4.  
  5. .DESCRIPTION
  6.     This script provides a detailed report of permissions set directly on a given folder.
  7.     It uses the System.IO.DirectoryInfo and System.Security.AccessControl classes
  8.     from the .NET Framework to get the DirectorySecurity object and enumerate
  9.     each FileSystemAccessRule that is not inherited from a parent folder.
  10.  
  11. .PARAMETER FolderPath
  12.     The full path to the folder you want to inspect.
  13.  
  14. .EXAMPLE
  15.     PS C:\> .\Get-FolderPermissions.ps1 -FolderPath "C:\Users\JohnDoe"
  16.     This will display the explicit permissions for the C:\Users\JohnDoe folder.
  17.  
  18. .NOTES
  19.     You must run this script with sufficient privileges to read the security
  20.     information of the target folder. Running as an Administrator is recommended.
  21. #>
  22. param (
  23.     [Parameter(Mandatory=$true, HelpMessage="Enter the full path to the folder.")]
  24.     [string]$FolderPath
  25. )
  26.  
  27. # --- 1. Validate the Folder Path ---
  28. Write-Host "Checking path: $FolderPath" -ForegroundColor Cyan
  29. if (-not (Test-Path -Path $FolderPath -PathType Container)) {
  30.     Write-Error "Error: The specified path '$FolderPath' does not exist or is not a directory."
  31.     # Exit the script gracefully if the path is invalid
  32.     return
  33. }
  34.  
  35. try {
  36.     # --- 2. Get the DirectoryInfo .NET Object ---
  37.     # Create a .NET object representing the directory.
  38.     $directoryInfo = New-Object System.IO.DirectoryInfo($FolderPath)
  39.     Write-Host "Successfully created DirectoryInfo object for '$($directoryInfo.FullName)'." -ForegroundColor Green
  40.  
  41.     # --- 3. Get the Access Control (DirectorySecurity) .NET Object ---
  42.     # GetAccessControl() returns the security descriptor for the directory.
  43.     # This can fail if the user running the script lacks permissions.
  44.     Write-Host "Attempting to retrieve Access Control List (ACL)..."
  45.     $directorySecurity = $directoryInfo.GetAccessControl()
  46.     Write-Host "ACL retrieved successfully.`n" -ForegroundColor Green
  47.  
  48.     # --- 4. Get the Collection of NON-INHERITED Access Rules ---
  49.     # The GetAccessRules() method retrieves the access rules contained in the security object.
  50.     # The parameters specify:
  51.     #   $true  - Include rules explicitly set on the object.
  52.     #   $false - DO NOT include rules inherited from parent objects.
  53.     #   [type] - The type to use for translating the Security Identifier (SID),
  54.     #            e.g., BUILTIN\Administrators instead of S-1-5-32-544.
  55.     $accessRules = $directorySecurity.GetAccessRules($true, $false, [System.Security.Principal.NTAccount])
  56.  
  57.     # --- 5. Display the Permissions Report ---
  58.     Write-Host "--- Non-Inherited Permissions Report for '$($directoryInfo.FullName)' ---" -ForegroundColor Yellow
  59.     Write-Host ("Found {0} explicit (non-inherited) access rules.`n" -f $accessRules.Count)
  60.  
  61.     if ($accessRules.Count -eq 0) {
  62.         Write-Host "No explicit permissions found. All permissions are inherited from parent folders." -ForegroundColor Magenta
  63.     }
  64.  
  65.     # Loop through each access rule and display its properties
  66.     foreach ($rule in $accessRules) {
  67.         Write-Host "Identity           : $($rule.IdentityReference.Value)"
  68.         Write-Host "Permissions        : $($rule.FileSystemRights)"
  69.         Write-Host "Type               : $($rule.AccessControlType)" # Allow or Deny
  70.         # IsInherited is now always false, so we don't need to display it.
  71.         Write-Host "Inheritance Flags  : $($rule.InheritanceFlags)" # How the rule is inherited by child objects/containers
  72.         Write-Host "Propagation Flags  : $($rule.PropagationFlags)" # How the inherited rule is propagated
  73.         Write-Host "--------------------------------------------------"
  74.     }
  75.  
  76. }
  77. catch {
  78.     # This block will execute if any command in the 'try' block fails,
  79.     # most commonly GetAccessControl() due to insufficient permissions.
  80.     Write-Error "An error occurred: $($_.Exception.Message)"
  81.     Write-Warning "Please ensure you are running PowerShell with sufficient privileges (e.g., 'Run as Administrator')."
  82. }
  83.  
Tags: Gemini
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement