Solved

Powershell script to overwrite older file version

Posted on 2013-01-29
3
2,299 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
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…
The viewer will learn how to dynamically set the form action using jQuery.

708 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

18 Experts available now in Live!

Get 1:1 Help Now