Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)}))
{}
Else
{
'STORE XXX HAS NOT RECEIVED ANY ORDERS IN THE PAST 7 DAYS'
}


$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)}))
{}
Else
{
'STORE XXX DID NOT RUN ITS BACKUP LAST NIGHT'
}
share|improve this question
3  
Please show your research effort. –  Entbark Nov 4 '13 at 18:21

1 Answer 1

up vote 2 down vote accepted

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)}
share|improve this answer
    
If you have powershell 3, Test-Path has options for NewerThan and OlderThan –  Eris Nov 4 '13 at 18:57
    
Thank you very much @Keith, with those items I was able to determine that I am going to use: Get-ChildItem c:\folder\*.* Where{$._LastWrittenTime -lt (Get-Date).AddDays(-8)} and Get-ChildItem c:\folder\*.* Where{$._LastWrittenTime -gt (Get-Date).AddDays(-1)} I am now reseaching how to output the information so we can see what stores out of the 90 fail this test for one or both of the commands. –  Seth Little Nov 4 '13 at 19:36
    
@Eris, good point. Having used the Where predicate so long for this sort of filtering, I keep forgetting that the functionality was added as dynamic parameters to Test-Path. –  Keith Hill Nov 8 '13 at 0:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.