Folder Permissions information using Powershell

Getting folder permissions information using Powershell will be very easy compared to the manual process.

If we have any requirement to track all access info information as part of  Quarterly Access Review. By using PowerShell script we can do it very easily.

Powershell will help you to export all the folder access into to Excel, table or notepad.

Input:

This Post I will explain passing input in 2 different methods.

  1.  $Paths=@("\\SSIS211\D$\Batch","\\SSIS311\D$\Batch")
  2. $path="C:\FolderAccess\Input.txt

In case 1, I will give all the paths in the $path variable.

In case 2, I will pass all the path in Input.txt file, I generally prefer case 2, which avoid changes in code.

 

folder permissions

 

Function:

In this scenario, I use Get-ChildItem to get the path info and Get-Acl to get access info for the path.

 

foreach($path in $paths)
{
$FolderInfo = Get-ChildItem -Directory -Path $Path -Force

Foreach ($FInfo in $FolderInfo)
{
$Folderpath=$FInfo.FullName
$SecurityList = Get-Acl -Path $Folderpath
$AccessList=$SecurityList.Access
write-host -ForegroundColor Yellow " $Folderpath security info detail fetching ......."

foreach ($Access in $AccessList)
{
$FolderName=$FInfo.Name
$FolderPath=$FInfo.FullName
$GroupOrUser=$Access.IdentityReference
$Permissions=$Access.FileSystemRights

$result="$path,$FolderName ,$FolderPath,$GroupOrUser,$Permissions"
Add-Content $output -Value $result
}
}
}

Output:

We can export the output in Excel which will be more easy to filter.

$output="C:\Projects\Automation\FolderPermissions\output.csv"

$result="$path,$FolderName ,$FolderPath,$GroupOrUser,$Permissions" 

Add-Content $output -Value $result

 

Final Code:

$paths="C:\Projects\Automation\FolderPermissions\input.txt"

$output="C:\Projects\Automation\FolderPermissions\output.csv"

if(Test-Path $output){

write-host " Outfile already exist in $output. Will Delete and Recreate new one"
Remove-Item $output -Force
}


foreach($path in $paths)
{
$FolderInfo = Get-ChildItem -Directory -Path $Path -Force


Foreach ($FInfo in $FolderInfo) 
{
$Folderpath=$FInfo.FullName
$SecurityList = Get-Acl -Path $Folderpath
$AccessList=$SecurityList.Access
write-host -ForegroundColor Yellow " $Folderpath security info detail fetching ......."

foreach ($Access in $AccessList)
{
$FolderName=$FInfo.Name
$FolderPath=$FInfo.FullName
$GroupOrUser=$Access.IdentityReference
$Permissions=$Access.FileSystemRights

$result="$path,$FolderName ,$FolderPath,$GroupOrUser,$Permissions"
Add-Content $output -Value $result
}

}

}

folder permissions
Output in Excel:

folder permissions
Note:

We can create Job in SQL Server and schedule to run the Script according to over requirement.

Note: To learn more about Get-Acl function, Please click below link.
Click Me

Add a Comment

Your email address will not be published. Required fields are marked *