Solved

Powershell script to overwrite older file version

Posted on 2013-01-29
3
2,342 Views
Last Modified: 2013-03-28
I'm looking for a Powershell script that will compare the date of an existing file on a user's pc to the date of the same file on a server file share.  If the version on the server is newer, I need it to overwrite the version on the user's pc.
0
Comment
Question by:labinformatics
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38831302
set your local and remote file in the variables:

$localfile = ''
$remotefile = ''

$localfiletime = [datetime](Get-ItemProperty -Path $localfile -Name LastWriteTime).lastwritetime
$remotefiletime = [datetime](Get-ItemProperty -Path $remotefile -Name LastWriteTime).lastwritetime

if($remotefiletime -gt $localfiletime){
	Copy-Item $remotefiletime $localfiletime
}

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38831310
btw, the script is checking last write date, if you want to check creation date then  replace LastWriteTime with CreationTime
0
 
LVL 2

Expert Comment

by:lalitgolani
ID: 38831315
This script will compare the date stamp of test.txt file placed locally with its server copy and if the server copy is newer than the local copy.. it will replace the file locally......

Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objLocalFile = objFSO.GetFile("c:\scripts\test.txt")
dtmLocalDate = objLocalFile.DateLastModified

Set objServerFile = objFSO.GetFile("\\atl-fs-01\public\test.txt")
dtmServerDate = objServerFile.DateLastModified

If dtmLocalDate < dtmServerDate Then
    objFSO.CopyFile objServerFile.Path, objLocalFile.Path, OverwriteExisting
End If
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now