Check if a file was modified after creation using powershell script

…this post was published on 2012 August 8.

First we need to check all files inside the folder. For this, you can use, this powershell command:

> Get-ChildItem -recurse
( if you need more infos regardind this command, use
> Get-Help Get-ChildItem, or go directly to MS site )

The answer will show all files, but by default only these columns

Mode                LastWriteTime         Length            Name
If you need all columns/attributes use this syntax:
> Get-ChilItem -recurse | Format-List *

For example, one of the files will look like

BaseName
Name
Length
DirectoryName
Directory
IsReadOnly
FullName
Extension
CreationTime
CreationTimeUtc
LastAccessTime
LastAccessTimeUtc
LastWriteTime
LastWriteTimeUtc
: filename
: -a—
: xx.txt
: 35722
: C:\–path
: C:\–path
: False
: True
: C:\–path\filename
: .txt
: 4/20/2012 1:54:52 PM
: 4/20/2012 10:54:52 AM
: 4/20/2012 1:54:52 PM
: 4/20/2012 10:54:52 AM
: 8/16/2012 11:27:31 PM
: 8/16/2012 8:27:31 PM
: Archive

As you can see, is a difference between LastAccessedTime, and LastWriteTime

Each time the get-ChildItem is executed,result an object for which we can find methods and properties attached. For this I will use:

> Get-ChildItem | Get-Member

You can se alos that we have a group of methods and Properties for Directories, and another one for files.

Let’s check deeper, for files; I’m interested about

LastAccessTime      Property       System.DateTime LastAccessTime {get;set;}
LastWriteTime        Property       System.DateTime LastWriteTime {get;set;}

Now let’s go deeper, and make more complicated the code to show files and folder, usign foreach() function:

 foreach ($filename in Get-ChildItem){
      “The  name is:” +$filename
    }

Which is the difference between folder and file? How get it? Simpe,. Use Attributes, like this:
 > foreach ($filename in Get-ChildItem) {
  “The name is:” +$filename
            if ( $filename.attributes -match “Directory” ){
           “It is a DIRECTORY”
            }
    }

And else, add a message like: “It is a File”
 >  foreach ($filename in Get-ChildItem) {
    “The name is:” +$filename
           if ( $filename.attributes -match “Directory” ){
          “It is a DIRECTORY”
           }
           else {
           “It is a FILE”
           }
      }

Now, let’s change how it shows:
 >   foreach ($filename in Get-ChildItem) {
           if ( $filename.attributes -match “Directory” ){
           “DIRECTORY:” + “`t”+$filename
            }
            else {
          “FILE:” +”`t”+$filename
           }
      }

Great!. Now, let’s show file’s infos about “LastWriteTime” , and “CreationTime”. For this i added more lines:
> foreach ($filename in Get-ChildItem) {
          if ( $filename.attributes -match “Directory” ){
          “DIRECTORY:” + “`t”+$filename
         }
         else {
        “FILE:” +”`t”+$filename
        “Creation Time:”+”`t”+$filename.CreationTime
        “Last Write Time:”+”`t”+$filename.LastWriteTime
         }
    }

Last step. Compare these 2 dates, and show the message:
 >   foreach ($filename in Get-ChildItem) {
           if ( $filename.attributes -match “Directory” ){
          “DIRECTORY:” + “`t”+$filename
            }
          else {
         “FILE:” +”`t”+$filename
         “Creation Time:”+”`t”+$filename.CreationTime
         “Last Write Time:”+”`t”+$filename.LastWriteTime
                  if ( $filename.LastWriteTime -ne $filename.CreationTime) {
                 “*****************************************”
                 “The file: “+ $filename +” changed after creation! “
                  }
       }
    }

This entry was posted in Powershell, Sharepoint 2010. Bookmark the permalink.