EDUCBA

EDUCBA

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

Powershell Copy File

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » Powershell Copy File

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.

Syntax of Powershell Copy File

The below is the syntax :

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

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,705 ratings)
Course Price

View Course

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

-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 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
  • 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
  • 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
  • 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
  • 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
  • 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.

Special Offer - Shell Scripting Course Learn More