I'm trying to find all unique file owners in an NTFS file system. Getting the number is fairly straightforward, but my current approac is very inefficient:
$Shares = @(
"\\Server\Share1",
"\\Server\Share2",
"\\Server\Share3",
"\\Server2\Share1",
...
)
$i = 0
foreach ($Share in $Shares) {
$i++
$AllFiles = Get-ChildItem -Path $Share -File -Recurse -Force -ErrorAction SilentlyContinue
$Output = $AllFiles | ForEach-Object { [System.IO.FileSystemAclExtensions]::GetAccessControl($_).Owner } | Group-Object | Sort-Object Name | Select-Object Count, Name
$Output | Out-File "C:\My Path\My file$i.csv"
}
I don't think there's any way around using [System.IO.FileSystemAclExtensions]::GetAccessControl() but is there some better, more efficient way of getting the unique file owners only? There are two issues I can see here:
- Storing the file info of every single file in the share in a single variable
- Running a
Group-Objectcommand on that massive data set
I thought about instead of using Group-Object storing them in a separate ArrayList and for each file see if the owner already exists but then I'd have to compare an increasingly large ArrayList with a value every time, so that feels like it would be exponentially slower than doing the Group-Object approach.
Are there better options doing this?