Shepard
Shepard

Reputation: 23

Powershell: How to get and export NTFS Permissions

I currently have been using the following script to get NTFS Permissions. The issue is that when it comes to larger shares, it is very RAM intensive.

# Root Share or Root path​
$RootShare = '<\\Share>'
# Gathering files and Directories
$Tree = Get-ChildItem -Path $RootShare -Recurse 
# Gathering NTFS permissions for the RootShare
$NTFS = Get-NTFSAccess -Path $RootShare
# Adding Files and SubDirectories NTFS Permissions
$NTFS += foreach ($Item in $Tree)
 { 
    #excluseInherited for a concise report
  Get-NTFSAccess -Path $Item.FullName #-ExcludeInherited #or -ExcludeExplicit
 }
# Export result to a file

$NTFS | Export-Csv -Path 'C:\Temp\File.csv' -NoTypeInformation


The issue is regarding larger shares, this is very RAM intensive. I have tried other methods such as appending ACL's to a CSV but I usually run into the same issue. This is because of $NTFS += foreach ($Item in $Tree) as I believe it just adds to the variable, and will not export until it has recursively finished.

Upvotes: 1

Views: 3803

Answers (1)

zett42
zett42

Reputation: 27766

Use the pipeline consequently and avoid temporary variables to keep memory footprint low. As an added benefit, the code becomes more concise.

# Root Share or Root path​
$RootShare = '<\\Share>'

# Start a scriptblock to group commands which are piped to Export-Csv
& {
    # Gathering NTFS permissions for the RootShare.
    # -> becomes implict output of the scriptblock
    Get-NTFSAccess -Path $RootShare

    # Gathering files and Directories. By piping Get-ChildItem
    # to Get-NTFSAccess we save another temporary variable.
    # -> becomes implict output of the scriptblock
    Get-ChildItem -Path $RootShare -Recurse |  
        Get-NTFSAccess #-ExcludeInherited #or -ExcludeExplicit

} | Export-Csv -Path 'C:\Temp\File.csv' -NoTypeInformation
    # Here we are piping the output of the scriptblock to Export-Csv

Upvotes: 1

Related Questions