Solved

Powershell script to overwrite older file version

Posted on 2013-01-29
3
2,607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

728 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