Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 PowerShell better). 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 seven 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" 

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' } 
like image 239
Seth Little Avatar asked Nov 04 '13 18:11

Seth Little


People also ask

How do I find the modified date of a folder?

How to find the date of modified files. Press the Windows key + E on the keyboard to open File Explorer. On the left side-scrolling menu, select the drive or folder that you want to view the last modified date(s) (A) for the contents.

How do I find the last modified date of a file?

The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.


1 Answers

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)} 
like image 85
Keith Hill Avatar answered Sep 21 '22 14:09

Keith Hill