none
PowerShell script to Export NTFS permission RRS feed

  • Question

  • I am looking for a script which will export NTFS permission to to excel. 

    below script will serve the purpose, but since I am running on a large share with millions of folder its running for days and not sure where its stuck. 

    1st day it showed me couple of error saying dont have access to this folder in PowerShell and after that it stopped showing any error but something is still running i guess but script not finished after 4 days. 

    what I am looking for is a script which will create excel first and keep writing to the excel rather that storing in table and and export at the end so i know if the script is progression or not?  

    $FolderPath = dir -Directory -Path "\\server\share" -Recurse -Force
    $Report = @()
    Foreach ($Folder in $FolderPath) {
        $Acl = Get-Acl -Path $Folder.FullName
        foreach ($Access in $acl.Access)
            {
                $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD
    Group or
    User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
                $Report += New-Object -TypeName PSObject -Property $Properties
            }
    }
    $Report | Export-Csv -path "C:\\Export NTFS Permission.csv"

    Monday, September 28, 2020 3:18 PM

All replies

  • Instead of appending elements to the $Report array and then exporting to csv at the end of the script, export each row/element to csv in the foreach loop. Use the -append switch to add a row of data to the existing csv file. I also recommend using the -NoTypeInformation switch with the export-csv cmdlet; this will write to csv without the first few rows of type information.

    To use in Excel, create a separate workbook and add this csv as a data source.

    Wednesday, September 30, 2020 5:40 AM
  • can you please  help modify ? 
    Friday, October 2, 2020 2:49 AM
  • Yes. In the modified script, a datarow is created in each iteration of the foreach loop ($item) and is appended to the csv file.

    Foreach ($Folder in $FolderPath) {
        $Acl = Get-Acl -Path $Folder.FullName
        foreach ($Access in $acl.Access)
            {
                $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD
    Group or
    User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
                $item = New-Object -TypeName PSObject -Property $Properties
    
            $item | Export-Csv -path "C:\\Export NTFS Permission.csv" -NoTypeInformation -Append
    
            }
    }


    Friday, October 2, 2020 2:26 PM