Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 600
Trinitrotoluene Avatar asked Jun 28 '13 14:06

Trinitrotoluene


People also ask

What is the use of Get-ChildItem?

Description. The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

How do I get LastWriteTime in PowerShell?

Use the Get-ChildItem LastWriteTime attribute to find the list of files with lastwritetime. Get-ChildItem in the PowerShell gets one or more child items from the directory and subdirectories.

How do I get a list of files in PowerShell?

PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function.


2 Answers

Try this:

(Get-ChildItem -Path c:\pstbak\*.* -Filter *.pst | ? {
  $_.LastWriteTime -gt (Get-Date).AddDays(-3) 
}).Count
like image 150
Dave Sexton Avatar answered Oct 14 '22 01:10

Dave Sexton


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)}
like image 43
LReeder14 Avatar answered Oct 14 '22 01:10

LReeder14