Dmitry Dorofeev
Dmitry Dorofeev

Reputation: 85

Include file owner to powershell pipe

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

Upvotes: 0

Views: 92

Answers (3)

Dmitry Dorofeev
Dmitry Dorofeev

Reputation: 85

$dir_to_look="F:\"    
$month_backdate=$(Get-Date).AddDays(-7)  
Get-Childitem $dir_to_look -Recurse | ? { !($_.psiscontainer) -and $_.LastWriteTime -gt $month_backdate } |
Select-object LastWriteTime, Directory, FullName, @{n='Owner';e={(Get-ACL $_.FullName).owner}}

Upvotes: 0

markg
markg

Reputation: 302

Use a hash table with Select-Object.

$dir_to_look="F:\"    
$month_backdate=$(Get-Date).AddDays(-7)  
Get-Childitem $dir_to_look -Recurse | ? { !($_.psiscontainer) -and $_.LastWriteTime -gt $month_backdate } |
    Select-object LastWriteTime, Directory, FullName, @{n='Owner';e={(Get-ACL $_.FullName).owner}}

Upvotes: 0

Keith Hill
Keith Hill

Reputation: 201602

Your Foreach-Object command should be:

... | Foreach-Object {Add-Member -Input $_ -Type NoteProperty -Name Owner -Value (Get-Acl $_.Fullname).Owner -PassThru} | Select-object LastWriteTime, Directory, FullName, Owner | ...

Add-Member is handy for adding properties (and methods) to objects.

Upvotes: 1

Related Questions