Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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.