?
Solved

Powershell script to overwrite older file version

Posted on 2013-01-29
3
Medium Priority
?
2,712 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 1500 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

770 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