Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to run a command that is supposed to delete anything with the "LastAccessTime" older than 30 days. However, when I run it, it just modifies the LastAccessTime and doesn't delete. Command is below.

Get-ChildItem '\servername\share\folder in the share' | ?{$.LastAccessTime -lt (Get-Date).AddDays(-30)} | ?{!$.psiscontainer} | Remove-Item

The account I'm running this as has access to the share, including delete access (tested this manually). If I take out | Remove-Item and change the days to be older than 1 day, it returns the files. Should I use something other than Remove-Item?

share|improve this question

1 Answer 1

Please avoid using aliases in your script here on serverfault, as they decrease the readability of scripts.

Apart from that, your missing some underscores, I'm guessing your script should read:

Get-ChildItem '\servername\share\folder in the share' | where {$_.LastAccessTime -lt (Get-Date).AddDays(-30)} | where {!$_.psiscontainer} | Remove-Item
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.