Can't get powershell to return where results from GCI using ACL

Posted by Rossaluss on Super User See other posts from Super User or by Rossaluss
Published on 2014-06-05T08:51:03Z Indexed on 2014/06/05 9:30 UTC
Read the original article Hit count: 281

I'm trying to get Powershell to list files in a directory that are older than a certain date and match a certain user. I've got the below script so far which gives me all the files older than a certain date and lists the directory and who owns them:

$date=get-date

$age=$date.AddDays(-30)

ls '\\server\share\folder' -File -Recurse | `
where {$_.lastwritetime -lt "$age"} | `
select-object $_.fullname,{(Get-ACL $_.FullName).Owner} | `
ft -AutoSize

However, when I try and use an additional where parameter to select only files owned by a certain user, I get no results at all, even though I know I should, based on the match I'm trying to obtain (as below):

$date=get-date

$age=$date.AddDays(-30)

ls '\\server\share\folder' -File -Recurse | `
where ({$_.lastwritetime -lt "$age"} -and {{(get-acl $_.FullName).owner} -eq "domain\user"}) | `
select-object $_.fullname,{(Get-ACL $_.FullName).Owner} | `
ft -AutoSize

Am I missing something? Can I not use the get-acl command in a where condition as I've tried to?

Any help would be appreciated.

Thanks

© Super User or respective owner

Related posts about powershell

Related posts about powershell-ise