Powershell to get folder and sub-folder permissions

I have a folder and I would like to get it and all of its sub-folders permissions.

 I used icacls \\server\folder\ /t /c > | export c:\permissions.txt , but it gives me the folders, sub-folders and all documents included which makes my text file close to 700MB.


Is there a way to just get folder and sub-folders only?
NathanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy LidbetterCommented:
Hi There,

Does this help? (Just change the $Rootpath value)

$OutFile = "C:\temp\Permissions.csv"
$Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags"
Add-Content -Value $Header -Path $OutFile 

$RootPath = "\\servername\foldername"

$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}

foreach ($Folder in $Folders){
	$ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access  }
	Foreach ($ACL in $ACLs){
	$OutInfo = $Folder.Fullname + "," + $ACL.IdentityReference  + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
	Add-Content -Value $OutInfo -Path $OutFile
	}}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NathanAuthor Commented:
After run that, I get about 4 access denied errors, and the CSV file only has the headers listed (Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags).


I was hoping I was only missing command or switch.
0
Guy LidbetterCommented:
I just tested it,  the script works perfectly.

Make sure you have the permissions needed to run against the folder you are checking.
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

NathanAuthor Commented:
I'm logged in as the local admin when I run it, is there something else I need to be checking? (I'm still a Powershell noob)
0
Mauro CazabonnetSenior Software EngineerCommented:
I added some try catch statements to cover some of the excpetions that may occur

$OutFile = "C:\temp\Permissions.csv"
Remove-Item $OutFile -ErrorAction SilentlyContinue
$Header = "Folder Path,Exception,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags"
Add-Content -Value $Header -Path $OutFile

$RootPath = "\\servername\foldername"

try
{
    $Folders = dir $RootPath -recurse 2>&1 | where {$_.psiscontainer -eq $true}
}
catch [System.Exception]
{
    $_.Exception.Message
}

foreach ($Folder in $Folders){
   
    try
    {
        $ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access  }
        $Exception = $false
      }
    catch [System.Exception]
    {
        $Exception = $true
        $SystemMessage = $_.Exception.Message
    }
    Finally
    {
        Foreach ($ACL in $ACLs)
        {
             if ($Exception -eq $false) {
            $OutInfo = $Folder.Fullname + "," + $Exception  + "," + $ACL.IdentityReference  + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
             }
           else {
            $OutInfo = $Folder.Fullname + "," + $Exception  + "," + $SystemMessage
           }
           Add-Content -Value $OutInfo -Path $OutFile
       }
    }
}
0
Guy LidbetterCommented:
Considering you are actually getting the access denied errors, the Local Admin may not have NTFS permissions to the folders.

Can you check the folder permissions of the root folders?

If you find someone has set explicit rules, you may be locked out unless you modify the permissions before running the script.
0
NathanAuthor Commented:
The last script worked great! Thank you very much for your assistance.
0
Mauro CazabonnetSenior Software EngineerCommented:
Kewl!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.