Powershell how to get acls from files/directories recursively also including "[" or "]"

Viewed 53014

I have the script below working for most of the files and folders but is not working for files and folders with "[" "]"

#Set variables
$path =  $args[0]
$filename = $args[1]
$date = Get-Date

#Place Headers on out-put file
$list = "Permissions for directories in: $Path"
$list | format-table | Out-File "C:\Powershell\Results\$filename"
$datelist = "Report Run Time: $date"
$datelist | format-table | Out-File -append "C:\Powershell\Results\$filename"
$spacelist = " "
$spacelist | format-table | Out-File -append "C:\Powershell\Results\$filename"

#Populate Folders Array
[Array] $folders = Get-ChildItem -path $path -force -recurse 

#Process data in array
ForEach ($folder in [Array] $folders)
{
#Convert Powershell Provider Folder Path to standard folder path
$PSPath = (Convert-Path $folder.pspath)
$list = ("Path: $PSPath")
$list | format-table | Out-File -append "C:\Powershell\Results\$filename"

Get-Acl -path $PSPath | Format-List -property AccessToString | Out-File -append "C:\Powershell\Results\$filename"

"-----------------------" | Out-File -FilePath "C:\Powershell\Results\$filename" -Append

} #end ForEach  
4 Answers

I made a slight change to @peter's one-liner. it speeds it up quite a bit:

Get-Childitem "C:\windows\temp" -folder -recurse | get-acl | Export-CSV "c:\temp\output.csv" -notype -append

Related