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

The code that is currently

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

Essentially, I'm trying to get a list of all the PST files in the above folder, based on them, newer than 3 days. I would like to count the results. The above code does not cause errors, but returns zero results (there are PST files in a folder that differs from three days. Does anyone have any ideas?

+6
source share
4 answers

Try the following:

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

It is very similar to the previous answers, but it is from the current directory, it scans any file and only for those who are 4 days old. This is what I need for my research, and the above answers were very helpful. Thanks.

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

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

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

It still does not return zero if empty, but testing '-lt 1' works.

+1
source

Here's a small update to the solution provided by Dave Sexton. Many times you need several filters. The Filter parameter can accept only one string, while the -Include parameter can accept a string array. if you have a large tree of files, it only makes sense to get a date for comparison with one, and not for each file. Here is my updated version:

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

Source: https://habr.com/ru/post/948325/


All Articles