- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy only updated or newer files with PowerShell?
To copy only updated or newer files with PowerShell, we can use Copy-Item with some logic in the script that to check if the files exist on the destination folder if not then copy that file and if yes then compare the timestamp and copy the latest file. This would be tricky because we need to write several lines of code for it.
But Windows support xCopy utility which can directly copy the newer or updated files and this utility we can accommodate in PowerShell as well.
xCopy uses a switch called /d. its actual syntax is,
Syntax
/d [:MM-DD-YYYY]
This means if the date specified, it copies the files and folders newer than that date and if don't specify any date then it copies all files and folders that are updated. An example is shown below.
Source:
C:\DSC
Destination:
E\Temp
Our folder structure is like this, LabMachine2k16.mof file also exists on the destination but it is an updated file, Test.txt is a new file and the Pagefile folder is a new folder but it is an empty one.
Example
xcopy C:\DSC E:\Temp\ /s /d /e /f /y
Output
PS C:\> xcopy C:\DSC E:\Temp\ /s /d /e /f /y C:\DSC\LabMachine2k16.mof -> E:\Temp\LabMachine2k16.mof C:\DSC\Test.txt -> E:\Temp\Test.txt 2 File(s) copied
Switches.
/s – Specifies the subfolder
/e – Specifies the empty directory
/d – Specifies the updated files to copy
/f – Displays the source and the destination.
/y – Confirms without asking user prompt.
- Related Articles
- How to copy files with the specific extension in PowerShell?
- How to copy multiple files in PowerShell using Copy-Item?
- How to copy files of the specific extension using PowerShell?
- How to Copy files or Folder without overwriting existing files?
- How to copy readonly and hidden files/folders in the PowerShell?
- How to copy files/folders to the remote location in the PowerShell?
- How to get only hidden files and folders in PowerShell?
- How to get only files but not the folders with Get-ChildItem using PowerShell?
- How to Zip / Unzip files or folders using PowerShell?
- How to copy a file, group of files, or directory in Linux?
- How to copy folder contents in PowerShell with –Recurse parameter?
- How to get only folders but not the files in the Get-ChildItem in PowerShell?
- How to Copy NTFS permissions using PowerShell?
- How to display files/folders including hidden files/folders in PowerShell?
- How to copy files from host to Docker container?
