EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell xcopy

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell xcopy

PowerShell xcopy

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

Popular Course in this category
Sale
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,725 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

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:

PowerShell xcopy 1

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:

copying with subfolders

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:

continue before starting the copy of files

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:

see the source and the destination file

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:

PowerShell xcopy 5

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:

PowerShell xcopy 6

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 –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

4th of July Offer - Shell Scripting Course Learn More