Ajay
Ajay

Reputation: 77

List of files not accessed for the last 1 year in shared folder using powershell

I am trying to get a list of files in shared folder not accessed for more than one year , i gave the filter AddMonths(-12) also tried AddYears(-1) , doesnt work, list generated in csv contains file having date as current year

$time = Get-Date
$time.AddMonths(-12)

get-childitem "\\Ajay-1\" -Recurse -File -ErrorAction SilentlyContinue| Where- 
Object {$_.LastAccessTime -lt $time} | select fullname,lastaccesstime |
export-csv "D:\Time\Last.csv" -notypeinfo

Upvotes: 0

Views: 1494

Answers (1)

Matthew
Matthew

Reputation: 1450

You'd have better luck modifying how you're handling $time

$time = (Get-Date).AddDays(-365)
$path = "C:\"

Get-ChildItem -Path $path -Recurse -Force  | Where-Object {$_.LastAccessTime -lt $time} | select fullname,lastaccesstime |export-csv "D:\Time\Last.csv" -notypeinfo

Using this should achieve what you're looking for.

Upvotes: 1

Related Questions