Include file owner to powershell report
Hi All,

I've got a script which works fine which lists all files modified since last 7 days and want to modify it to add file owner to the export csv file. 

$dir_to_look="F:\"    
$month_backdate=$(Get-Date).AddDays(-7)    
Get-Childitem $dir_to_look -Recurse | ? { !($_.psiscontainer) -and $_.LastWriteTime -gt $month_backdate } | ForEach-Object {Get-Acl $_.FullName}.owner | Select-object LastWriteTime, Directory, FullName  |  export-csv -path \\sharename\report.csv -encoding "unicode"



But not sure how to correctly add get-acl to the pipe as currently it prints nothing to my report file
July 28th, 2015 11:08am

Hi,

Here's something you can play with:

$path = '.\'
$dateCutoff = (Get-Date).AddDays(-7)
Get-ChildItem -Path $path -Recurse -File | Where { $_.LastWriteTime -gt $dateCutoff } | ForEach {

    $props = @{
        LastWriteTime = $_.LastWriteTime
        Directory = $_.Directory
        FullName = $_.FullName
        Owner = (Get-Acl $_.FullName).Owner
    }

    New-Object PsObject -Property $props

} | Export-Csv .\outputFile.csv -NoTypeInformation

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 12:17pm

FYI - this is faster:

$_.GetAccessControl().Owner

July 28th, 2015 12:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics