[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Powershell script to overwrite older file version

Posted on 2013-01-29
3
Medium Priority
?
2,844 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:
Meir Rivkin 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:Meir Rivkin
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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

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 …
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

649 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