Hello Everyone,
I recently came across a task, where in I had to get folder permissions using Powershell on a specified path and all the sub folders.
Well, I could have done this using the GUI. But imagine how difficult it was going to be if I had multiple sub folders and putting it in a readable was just going to be too cumbersome.
That's when I jumped to Powershell. We know that Windows stores the information related to File permissions for an object in Access Control Lists (ACLs).
So I just had to head over and type Get-Help *ACL | Format-Table -Autosize - Wrap
to find the related cmdlets available to us.
Now we are presented with two cmdlets Get-ACL and Set-ACL along with their descriptions.
It is pretty clear now that we will be using Get-ACL to retrieve the required information.
Let us say that we want to find the permissions for the sub folders in Windows Folder on C drive.
To Find the subfolders, we will be using Get-ChildItem, which is similar to dir from Command Prompt or ls if you are a Linux guy.
Get-ChildItem .\Windows | where-object {($_.PsIsContainer)} | Get-ACL | Format-List
Note that we have used PsIsContainer as a filter. The where-object {($_.PsIsContainer)} will make sure only the directory is shown. Without this filter, all the files and folders will show up on your result.
If you would like to research more on the Get-ACL properties, here is an handy one-liner.
Get-Acl C:\Windows | Get-Member -MemberType *Property
MemberType *Property filters out methods, and shows just the various properties.
If we need to find the permissions on each and every sub folder then we will need to -Recurse parameter along with Get-ChildItem. Therefore, the cmdlet will look like.
Get-ChildItem .\Windows -Recurse | where-object {($_.PsIsContainer)} | Get-ACL | Format-List
And we have to output the results to a text file, the cmdlet will be as below.
Get-ChildItem .\Windows -Recurse | where-object {($_.PsIsContainer)} | Get-ACL | Format-List | Out-File C:\Permissions.txt
That's it for today! I hope this has been informative and of value and thank you for reading.