Seth Little Seth Little - 1 year ago 113
PowerShell Question

Powershell Script for Finding Modified Date of a file/folder

I am very new to Powershell and I was hoping I could get some help creating a script that tells me the modified date of a file. I wish I knew more about Powershell, as I feel like I am asking a lot (all my free-time this week will be dedicated to learning PS better). Any help would be greatly appreciated. Pointing me in the direction of where to learn how to do this would be very helpful as well.

Here is the complete rundown. I need to run a report daily that checks a list of computers at 90 different stores to make sure their a certain backup was performed. The modified date should tell if the backup had been performed, and will be set to the previous date. If the modified date is yesterday, then there does not need to be an output. If it is not yesterday, I would like to have the output in the powershell window, or to a text file, whichever would be easier.

I also have to check that a folder is no older than 7 days for each of the 90 stores, with the same criteria for the output.
The idea that I have would be like this for each store
For Store 1
check file date for \server\store\computer\c:\folder\"newest modified date in folder"
if date equals yesterday
then do nothing
if date does not equal yesterday
then output "Test did not backup"

check folder modified date for \server\sample\store\backupfolder
if date equals <7 days old
then do nothign
if date equals >7 days old
then output "test did not backup"

EDIT: Sorry for not Proving my research effort, I am very new to Powershell and I was on a deadline to get this done. I have, since yesterday, learned how to do everything that I needed to do with this script. Thanks to @Keith for setting me on the correct path. I ended up using the following code to accomplish my goal of only out-putting the location where result was false.

$a = Get-ChildItem \\server\XXX\Received_Orders\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-7)}
if ($a = (Get-ChildItem \\server\XXX\Received_Orders\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-7)}))

$b = Get-ChildItem \\COMP NAME\Folder\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-1)}
if ($b = (Get-ChildItem \\COMP NAME\TFolder\*.* | Where{$_.LastWriteTime -ge (Get-Date).AddDays(-1)}))

Answer Source

If you run the Get-Item or Get-ChildItem commands these will output System.IO.FileInfo and System.IO.DirectoryInfo objects that contain this information e.g.:

Get-Item c:\folder | Format-List  

Or you can access the property directly like so:

Get-Item c:\folder | Foreach {$_.LastWriteTime}

To start to filter folders & files based on last write time you can do this:

Get-ChildItem c:\folder | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-7)}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download