Updated March 6, 2023
Introduction to PowerShell xcopy
The following article provides an outline for PowerShell xcopy. The xcopy command (as the name suggests) copies files from the source location to the destination location including files, folders, and recursively as well and it is quite different than the simple copy command because it supports the multiple switches including the copying the directory structure to the different attributes like read-only, hidden, systems files and also provides the resilience over server offline to resume its work again.
Syntax:
xcopy is a simple command for files and folders copy with different attributes.
This command can be used with single or multiple attributes and its syntax is as shown below:
Xcopy <Source> [<Destination>] [/w] [/p] [/c] [/v] [/q] [/f] [/l] [/g] [/d [:MM-DD-YYYY]] [/u] [/i] [/s [/e]] [/t] [/k] [/r] [/h] [{/a | /m}] [/n] [/o] [/x] [/exclude:FileName1[+[FileName2]][+[FileName3]] [{/y | /-y}] [/z] [/b] [/j]
How does xcopy Command Work in PowerShell?
xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command. While copying files and folders, you can provide various switches as per your requirement to copy the data from the source to the destination location and this command works similar to Copy-Item, Copy, Robocopy, etc with the advanced switches.
When we use the xcopy command in a batch file and deal with the copy errors, the xcopy command uses the ErrorLevel parameter.
Below are exit codes for the error level:
Exit Code |
Description |
0 | Files are copied successfully without any error. |
1 | No files were found to copy. |
2 | Operation terminated by user by pressing Ctrl + C. |
3 | Error occurred due to invalid syntax, wrong source or destination path, disk or memory error. |
4 | Disk write error occurred. |
Examples of PowerShell xcopy
Given below are the examples of PowerShell xcopy:
Example #1
Copying files with xcopy command.
The below command will copy files to the destination. In this example, files are copied from the C:\DSC folder to E:\Temp. This is the simple command, which only copies parent files but not the subdirectories and their files.
Code:
xcopy C:\DSC\* E:\temp
If the destination folder doesn’t exist, this command asks if it can create a destination folder and if you press D, it creates a destination folder and copies files.
Output:
Example #2
xcopy for copying with subfolders.
xcopy command without switch doesn’t copy the subfolder and subfolder files. To include subfolders and their contents except for the empty directory, use /s switch.
Code:
xcopy C:\DSC\* E:\temp /s
Output:
The above command doesn’t include the empty directories. To include an empty directory, use /e switch.
Code:
xcopy C:\DSC\* E:\temp /s /e
Example #3
Copy hidden files and folders.
The below command will copy the hidden files and folders including the subfolder content and empty folders.
Code:
xcopy C:\DSC\* E:\temp /s /h /e
Example #4
Other xcopy switches.
a. /w: This switch prompts for the user consent to continue before starting the copy of files as shown below.
Code:
xcopy C:\DSC\* E:/temp /s /w
Output:
b. /f: To see the source and the destination file location while copying, use the/f switch.
Code:
xcopy C:\DSC\* E:\temp /s /f
Output:
The above command copies files from the source to the destination including the sub-folder and displays the individual item’s source and destination location.
c. /l: When you specify this switch, it displays the list of items to be copied to the destination folder without copying them.
Code:
xcopy C:\DSC\* E:\temp /s /l
Output:
d. /d [:MM-DD-YYYY]: Copies the newer files that are updated on or after the specific mentioned date. When you don’t provide the date, it copies all the files that are updated.
Code:
xcopy C:\DSC\* E:\Temp\ /s /d /f
The above commands copy all the files from the source to the destination which are changed or updated and also show the source and destination location.
Output:
If you have noticed, it asks for the user confirmation to proceed. To suppress that alert, you can use the/y switch.
Code:
xcopy C:\DSC\* E:\Temp\ /s /d /y /f
Conversely, /-y switch is used for the user prompt.
/d switch doesn’t only copy the updated file but it also copies the newer files to the destination along with it. Below command copies, the files which are updated after 31st Dec 2020.
Code:
xcopy C:\DSC\* E:\Temp\ /s /d:12-31-2020 /y
e. /h: To copy hidden files and system files that are not included in the xcopy command by default.
Code:
xcopy C:\DSC E:\Temp\ /s /h /y /f
The above command copies files from the source to the destination along with subfolders, system files, and hidden files without user confirmation.
Similarly, /r switch copies the read-only files. The below command will copy both hidden and read-only files.
Code:
xcopy C:\DSC E:\Temp\ /s /h /r /y /f
f. /z: Copies files over a network in restartable mode. This means that if the remote server goes offline, this switch keeps the track of the files that are copied and resumes operation when a server comes online. It also shows the percentage for the larger files that how much it copied.
Code:
xcopy C:\DSC \\ad\shared\temp /s /z
g. /Exclude: To exclude the specific items, we need to create a list in a text file with the item names to exclude.
In the below example, File excludeditems.txt contains a list of items to exclude.
We can use the command below to copy items to the destination with subfolders.
Code:
xcopy C:\DSC E:\Temp\ /s /exclude:C:\excludeditems.txt
Conclusion
Various commands like Copy, xcopy, RoboCopy, Copy-Item can be used to copy files and folders from the source to destinations but xcopy and Robocopy offers more functionality and can be run from the batch file and as well as from the PowerShell scripts with the strong support of switches as mentioned making it easier to use and easy to recover from fatal recovery.
Recommended Articles
This is a guide to PowerShell xcopy. Here we discuss the introduction, how does xcopy command work in PowerShell? and examples respectively. You may also have a look at the following articles to learn more –