Trinitrotoluene
Trinitrotoluene

Reputation: 1458

Using Get-childitem to get a list of files modified in the last 3 days

Code as it is at the moment

get-childitem c:\pstbak\*.* -include *.pst | Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-3)} |

Essentially what I am trying to do is get a list of all PST files in the folder above based on them being newer than 3 days old. I'd then like to count the results. The above code doesn't error but brings back zero results (there are definitely PST files in the folder that are newer than three days. Anyone have any idea?

Upvotes: 27

Views: 157337

Answers (6)

Smeterlink
Smeterlink

Reputation: 786

If you want to list the files and not only count them:

Get-ChildItem "C:\Program Files\*.*" -Recurse | 
Foreach {
$lastupdatetime=$_.LastWriteTime
$nowtime = get-date
if (($nowtime - $lastupdatetime).totaldays -le 3)
{
Write-Host "File modified: "$_
}
}

Key factor is to use " to avoid problems with spaces, fact which nobody else mentioned. Replace C:\Program Files with any path you like. If you want only the name use $_.Name instead of $_.

Upvotes: 0

Patrick Burwell
Patrick Burwell

Reputation: 173

By the title of the question the answer to find every file modified today is: Get-ChildItem -Path .*.pst| ? {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}

Modified in the last three days: Get-ChildItem -Path .*.pst| ? {$_.LastWriteTime -gt (Get-Date).AddDays(-3)}

Modified in the last three days and count: (Get-ChildItem -Path .*.pst| ? {$_.LastWriteTime -gt (Get-Date).AddDays(-3)}).count

Upvotes: 0

LReeder14
LReeder14

Reputation: 618

Very similar to previous responses, but the is from the current directory, looks at any file and only for ones that are 4 days old. This is what I needed for my research and the above answers were all very helpful. Thanks.

Get-ChildItem -Path . -Recurse| ? {$_.LastWriteTime -gt (Get-Date).AddDays(-4)}

Upvotes: 34

Yooakim
Yooakim

Reputation: 1817

Here's a minor update to the solution provided by Dave Sexton. Many times you need multiple filters. The Filter parameter can only take a single string whereas the -Include parameter can take a string array. if you have a large file tree it also makes sense to only get the date to compare with once, not for each file. Here's my updated version:

$compareDate = (Get-Date).AddDays(-3)    
@(Get-ChildItem -Path c:\pstbak\*.* -Filter '*.pst','*.mdb' -Recurse | Where-Object { $_.LastWriteTime -gt $compareDate}).Count

Upvotes: 3

Jonman364
Jonman364

Reputation: 11

I wanted to just add this as a comment to the previous answer, but I can't. I tried Dave Sexton's answer but had problems if the count was 1. This forces an array even if one object is returned.

([System.Object[]](gci c:\pstback\ -Filter *.pst | 
    ? { $_.LastWriteTime -gt (Get-Date).AddDays(-3)})).Count

It still doesn't return zero if empty, but testing '-lt 1' works.

Upvotes: 1

Dave Sexton
Dave Sexton

Reputation: 11188

Try this:

(Get-ChildItem -Path c:\pstbak\*.* -Filter *.pst | ? {
  $_.LastWriteTime -gt (Get-Date).AddDays(-3) 
}).Count

Upvotes: 62

Related Questions