Updated March 4, 2023
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 :
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:
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 –