Watch files for update ad copy from \\remote to local directory

I would like to watch a folder and copy changed files to local directory and prepend date-time stamp to file name.

Can you direct me toward a good method to do this. Something in Powershell would be awesome!

Thank you,
John DarbyPMAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I had posted on a previous EE PAQ a link to a basic how-to implement a FileSystemWatcher using powershell which is available at Technet.

Original source is available at -

Here is a simple modified version -
#By BigTeddy 05 September 2011 
#This script uses the .NET FileSystemWatcher class to monitor file events in folder(s). 
#The advantage of this method over using WMI eventing is that this can monitor sub-folders. 
#The -Action parameter can contain any valid Powershell commands.  I have just included two for example. 
#The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true. 
#You need not subscribe to all three types of event.  All three are shown for example. 
# Version 1.1 
$folder = 'c:\_admin\test' # Enter the root path you want to monitor. 
$filter = '*.*'  # You can enter a wildcard filter here. 
$copyTo = 'c:\_admin\test2\'
$lastChangedTime = [DateTime]::Now
# In the following line, you can change 'IncludeSubdirectories to $true if required.                           
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 
# Here, we register the changed event: 
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action { 
    $span = [TimeSpan]::Parse([DateTime]::Now.Subtract($lastChangedTime)).TotalSeconds
    $path = $Event.SourceEventArgs.FullPath
    $name = $Event.SourceEventArgs.Name 
    $changeType = $Event.SourceEventArgs.ChangeType 
    $timeStamp = $Event.TimeGenerated
    $outFile = [IO.Path]::Combine($copyTo, [String]::Format("{0:yyyyMMdd@hhmmss}_{1}", $timeStamp, $name))

    # Here, we compare the current span of time between the last change time
    # and now to prevent multiple event handles
    if ($span -gt 2)
            Write-Host "The file '$name' was $changeType at $timeStamp" -fore white 
            Write-Host "Copying '$path' to '$outFile'" -fore red
            if (![System.IO.Directory]::Exists($copyTo))
            Copy-Item $path $outFile
            $lastChangedTime = [DateTime]::Now
        catch [System.Exception]
            Write-Host $_
# To stop the monitoring, run the following commands: 
# Unregister-Event FileChanged

Open in new window

Test output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John DarbyPMAuthor Commented:
Splendid; thank you for a good solution and helpful explanation!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.