Skip to content

Instantly share code, notes, and snippets.

@dotps1
Last active January 5, 2024 08:22
Show Gist options
  • Select an option

  • Save dotps1/9123328 to your computer and use it in GitHub Desktop.

Select an option

Save dotps1/9123328 to your computer and use it in GitHub Desktop.
Formats some acl properties to nice, readable csv columns.
<#
.SYNOPSIS
Gets useful ACL properties and exports them to a CSV file.
.DESCRIPTION
Gets Path, Owner, Access, Inheritance, and InheritanceFlags of an object(s) and export them to a CSV.
The CSV format is desinged for user firendly read-ablitly.
The first two columns of a row will be Path and Owner, then the next row(s) will be the Identity and Permissions of the left most object.
Once the next object is enumerated, the Path and Owner will begin on the left most column.
Example Output of 'Export-AclToCsv -Path "\\Server\Share" -ExportPath $env:HOMEPATH + "\test.csv":
PATH OWNER FileSystemRights IdentityReference IsInherited InheritanceFlags
\\Server\Share\Folder1 mydomain.org\Owner_sAMAccountName
FullControl UserNameA True ContainerInherit, ObjectInherit
FullControl UserNameB False ContainerInherit, ObjectInherit
\\Server\Share\Folder2 mydomain.org\Owner_sAMAccountName
Modify UserNameC True ContainerInherit, ObjectInherit
\\Server\Share\Folder3 mydomain.org\Owner_sAMAccountName
FullControl UserNameD True ContainerInherit, ObjectInherit
.EXAMPLE
Export-AclToCsv -Path $env:HOMEPATH -ExportPath $env:HOMEPATH + "\HomePathPermissions.csv"
.EXAMPLE
Export-AclToCsv -Path $env:HOMEPATH -ExportPath $env:HOMEPATH + "\HomePathPermissions_Files.csv" -FilesOnly -Recurse
.EXAMPLE
Export-AclToCsv -Path $env:HOMEPATH -ExportPath $env:HOMEPATH + "\HomePathPermissions_Folders.csv" -FoldersOnly -Recurse
.NOTES
CSV output formating works best with true spreadsheet application.
.LINK
https://gist.github.com/PowerShellSith
#>
function Export-AclToCsv
{
[CmdletBinding()]
[OutputType([Void])]
Param
(
# Path, Type String, Directory to enumerate ACL entries against.
[Alias("Dir")]
[ValidateScript({ Test-Path $_ -PathType Container })]
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeLine = $true)]
[String]
$Path,
# ExportPath, Type String, Fully Qualified Name of csv file to export to.
[Alias("Csv")]
[ValidateScript({ Test-Path (($_ -split '\\')[0..(($_ -split '\\').count -2)] -join '\') })]
[Parameter(Mandatory = $true,
Position = 1)]
[String]
$ExportPath,
# FoldersOnly, Type Switch, Only enumerate directorys in $Path.
[Parameter(Position = 2)]
[Switch]
$FoldersOnly = $false,
# FilesOnly, Type Switch, Only enumerate files in $Path.
[Parameter(Position = 3)]
[Switch]
$FilesOnly = $false,
# Recurse, Type Switch, Recursivly enumerate $Path.
[Alias("R")]
[Parameter(Position = 4)]
[Switch]
$Recurse = $false
)
Begin
{
if ($PSBoundParameters.ContainsValue($FoldersOnly) -and $PSBoundParameters.ContainsValue($FilesOnly))
{
throw "Cannot use both -FoldersOnly and -FilesOnly cmdlet switches together."
}
$TableFormat = @{ Name = "Path"; Expression = { $_.Path.ToString().SubString($_.Path.ToString().IndexOf(":") + 2) } },
@{ Name = "Owner"; Expression = { $_.Owner } },
@{ Name = "FileSystemRights"; Expression = { $_.FileSystemRights } },
@{ Name = "IdentityReference"; Expression = { $_.IdentityReference } },
@{ Name = "IsInherited"; Expression = { $_.IsInherited } },
@{ Name = "InheritanceFlags"; Expression = { $_.InheritanceFlags } }
}
Process
{
$gciParams = @{
Path = $Path;
ErrorAction = 'SilentlyContinue'
}
if ($FoldersOnly)
{
$gciParams.Add('Directory', $true)
}
if ($FilesOnly)
{
$gciParams.Add('File', $true)
}
if ($Recurse)
{
$gciParams.Add('Recurse', $true)
}
$items = Get-ChildItem @gciParams
}
End
{
foreach ($item in $items)
{
try
{
($acl = Get-Acl -Path $item.FullName) | Select-Object $TableFormat | Export-Csv -Path $ExportPath -NoTypeInformation -Append
foreach ($user in $acl.Access)
{
$user | Select-Object $TableFormat | Export-Csv -Path $ExportPath -NoTypeInformation -Append
}
}
catch
{
$item | Select-Object @{ Name = "Path"; Expression = { $_.FullName } },
@{ Name = "Owner"; Expression = { "UNABLE TO ENUMERATE" } },
@{ Name = "FileSystemRights"; Expression = { "UNABLE TO ENUMERATE" } },
@{ Name = "IdentityReference"; Expression = { "UNABLE TO ENUMERATE" } },
@{ Name = "IsInherited"; Expression = { "UNABLE TO ENUMERATE" } },
@{ Name = "InheritanceFlags"; Expression = { "UNABLE TO ENUMERATE" } } | Export-Csv -Path $ExportPath -NoTypeInformation -Append
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment