EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Powershell Copy File

Powershell Copy File

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Powershell Copy File

Introduction to Powershell Copy File

Copying files is one of the most trivial operations to be performed for any user. There are commands for it in all the shell languages, similarly, in PowerShell, this is achieved with the help of Copy-Item cmdlet. This cmdlet is not only to copy a single file, but it can also be used to copy a folder, copy multiple files inside a folder recursively. It also allows users to select and copy only files based on wild cards. This article will cover in detail the various ways in which this cmdlet can be used to copy items in PowerShell along with appropriate examples and sample scripts. In this topic, we are going to learn about Powershell Copy File.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of Powershell Copy File

The below is the syntax :

NAME
Copy-Item
SYNTAX
Copy-Item [-Path] <string[]> [[-Destination] <string>] [-Container] [-Force] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-FromSession <PSSession>] [-ToSession <PSSession>]  [<CommonParameters>] Copy-Item [[-Destination] <string>] -LiteralPath <string[]> [-Container] [-Force] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-FromSession <PSSession>] [-ToSession <PSSession>]  [<CommonParameters>] ALIASES
cpi
cp
copy

Parameters

Here are the following parameters mention below

-Confirm: This is used to get confirmation from the user before running the cmdlet. Its type is switch parameter. Its alias is cf. Its default value is false. It doesn’t accept pipeline input and wild card characters are not allowed. This is an optional parameter.

-Container: This parameter denotes that the container objects are preserved by the cmdlet while executing the Copy files operation. Its type is switch and its default value is true.It doesn’t accept pipeline input and wild card characters are not allowed. This is an optional parameter.

-Credential: This is used to elevate the credentials or impersonate another user while executing the command. This is not supported by any inbuilt providers. Its type is PSCredential. It accepts pipeline input whereas wild card characters are not accepted. This is an optional.

-Destination: This denotes the destination to where the copied file must be moved. Its type is string. Its default value is the current directory.It accepts pipeline input whereas wild card characters are not accepted. If the file name must be renamed, a new name should be mentioned.

-Exclude: This specifies the items that needs to be excluded from copying. It can be a path or a pattern. Wildcard characters are permitted. Its type is String[]. Default value is none. Pipeline input is not accepted whereas wild card are characters are allowed. This is optional.

-Filter: This denotes the filter to be used with the cmdlet. The only provider which supports this is the builtin FileSystem provider. Filters are more efficient that wild cards. Its type is string. Its default value is none.Pipeline input is not accepted whereas wild card are characters are allowed. This is optional.

-Force: This should be used while working with a read only file or alias. Its type is switch. Its default value is false. It doesn’t accept pipeline input and wild card characters are not allowed. This is an optional parameter.

-Include: This denotes the list of items that needs to be included.It can be a path or a pattern. Wildcard characters are permitted. Its type is String[]. Default value is none. Pipeline input is not accepted whereas wild card are characters are allowed. This is optional.

-LiteralPath: It denotes the path to one or more locations. It should be used as how it is typed exactly. Wild card path is not accepted. Escape characters must be enclosed within single quotes. Its type is String[]. Its aliases are PsPath and LP. Its default value is none. It accepts pipeline input.

-PassThru: This doesn’t generate any output. Its type is switch.It doesn’t accept pipeline input and wild card characters are not allowed. This is an optional parameter.

-Path: This is a mandatory parameter. It denotes the path where the items to be copied are available. Its type is String[]. Its default value is none. It accepts pipeline input and wild card characters are allowed.

-Recurse: This denotes for recursive action to take place. Its type is switch. The default value is false.It doesnt accept pipeline input and wild card characters are not allowed. This is an optional parameter.

-ToSession: This is used when a file is being copied to a remote machine. The destination is on the remote machine. Its type is PSSession. Its default value is none. Pipeline input and wild card characters are not accepted. This is optional.

-Whatif: This is used to see how the output will look like if the cmdlet is run. Its type is switch.It doesnt accept pipeline input and wild card characters are not allowed. This is an optional parameter.

Example of Powershell Copy File

Here is the following example mention below:

Input:

Write-Host "Welcome to the demo of Copy-File cmdlet"
Write-Host "Simple example of moving a file"
Copy-Item "C:\Vignesh\append.txt" -Destination "C:\Copy-File Eg"
Write-Host "File moved"
Write-Host "Copying all txt files in a location"
Copy-Item "C:\Vignesh\*txt" -Destination "C:\Copy-File Eg" -Recurse
Write-Host "Files are copied to the new location"
Get-ChildItem "C:\Copy-File Eg"
Write-Host "Copying a file and renaming while moving"
Copy-Item "C:\Vignesh\Messaging_August 2019_Roster.xlsx" -Destination "C:\Copy-File Eg\roas.csv"
Write-Host "File copied with a new name"
Get-ChildItem "C:\Copy-File Eg"
Write-Host "Copy files to a new directory"
Copy-Item "C:\Vignesh\*txt" -Destination "C:\Copy-File Eg\newdirectory" -Recurse
Write-Host "Files copied to new directory"
Get-ChildItem "C:\Copy-File Eg\newdirectory"
Write-Host "Copying to a remote machine"
$Session = New-PSSession -ComputerName "testserver" -Credential "test\vignesh"
Copy-Item "D:\test.log" -Destination "C:\" -ToSession $Session
Write-Host "File copied to remote machine"

Output:

Powershell Copy File output 1

Powershell Copy File output 2

output 3

Conclusion

Thus, the article covered about how files are copied in PowerShell using the Copy-File cmdlet in detail. It explained the various parameters that are associated with the cmdlet, their types and their usage. The article also demonstrated how to copy a single file, multiples files, renaming a file while moving, how to copy files to a remote computer, etc. using a sample script. To learn more about the cmdlet, it would be advisable to write sample scripts and practice with them.

Recommended Articles

This is a guide to Powershell Copy File. Here we discuss the Example of Powershell Copy File along with the parameters and syntax. You may also have a look at the following articles to learn more –

  1. PowerShell Get-Item
  2. PowerShell Split String
  3. PowerShell Get-Process
  4. String in PowerShell
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle2000+ Hour of HD Videos | 80 Learning Paths | 400+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

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

EDUCBA

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

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

*Please provide your correct email id. Login details for this Free course will be emailed to you
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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more