How to use PowerShell to Copy a File?
Requirement: Copy a File in PowerShell.
How to Copy a File using PowerShell?
PowerShell is a powerful scripting tool that enables administrators to automate tasks with ease, including copying files. In this guide, we will go over the steps to copy a file using PowerShell, including how to copy a single file, copy all files from one folder to another, how to copy and rename files, etc.
In PowerShell, you can use the Copy-Item
cmdlet to copy a file from one location to another. The basic syntax for the cmdlet is as follows:
Copy-Item -Path <source> -Destination <destination>
This cmdlet takes two parameters: the source location of the file and the destination location.
-Path
specifies the path of the file you want to copy. This can be a local or a remote file path.-Destination
specifies the location where you want to copy the file to. This can also be a local or a remote path.
For example, if you want to copy a file named “example.txt” from the “C:\Source” folder to the “C:\Destination” folder, you would use the following command:
Copy-Item -Path "C:\Source\example.txt" -Destination "C:\Destination"
PowerShell to copy and rename a File
You can copy a file to the same path or to a different folder to rename it. E.g.,
Copy-Item -Path "C:\Source\example.txt" -Destination "C:\Destination\NewExample.log"
#Also works: Copy-Item -Path "C:\Source\example.txt" -Destination "C:\Source\example-v2.txt"
Please note, the above script overwrites the destination file if it exists!
Ensure the Destination Folder Before Copying the Files
If the destination folder does not exist, the cmdlet will throw an error “Copy-Item : Could not find a part of the path <Path>”. To avoid this, you have to use the combination of Test-Item, New-Item, and Copy-Item cmdlet, to create the entire destination folder tree if it does not exist.
#Function to copy a File to given path
Function Copy-File {
[CmdletBinding()]
Param
(
# The path to the Source File location
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[string] $SourceFilePath,
# The destionaion Folder where the file to be copied
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=1)]
[string] $DestinationFolderPath
)
#Check if the Destination Folder exists
If (-not (Test-Path $DestinationFolderPath)) {
New-Item -ItemType Directory -Path $DestinationFolderPath | Out-Null
Write-Host "Ensured the $DestinationFolderPath!" -f Yellow
}
#Copy the File to Destination Folder
Copy-Item -Path $SourceFilePath -Destination $DestinationFolderPath
Write-Host "File $SourceFilePath Has been Copied to $DestinationFolderPath!" -f Green
}
#Call the function
Copy-File -SourceFilePath "C:\Temp\AppLog.zip" -DestinationFolderPath "C:\Temp2\Logs"
Copy Multiple Files in PowerShell
You can also use wildcard characters to copy multiple files at once. For example, the following command will copy all the text files from the “C:\Source” folder to the “C:\Destination” folder.
Copy-Item -Path "C:\Source\*.txt" -Destination "C:\Destination"
Similarly, to copy all files (without any subfolders and their files) from the source to the destination folder, use the following:
Copy-Item -Path "C:\Source\*.*" -Destination "C:\Destination"
Copy All Files and Folders Recursively
The Copy-Item cmdlet can also be used to copy an entire directory with its contents. To do this, you would use the -Recurse
parameter, which tells PowerShell to copy the contents of the directory along with its files and subdirectories.
Copy-Item -Path "C:\Source" -Destination "C:\Destination" -Recurse -Force
Please note, you have to use the -Force
switch merge with the destination folder if it exists already! The above cmdlet will merge the source with destination folders, and overwrite any files with the same name. Otherwise, You’ll get “Copy-Item : An item with the specified name C:\Temp2\Logs already exists.” error.
What if you want to copy the contents of a folder without the Root folder? Use:
Copy-Item -Path "C:\Temp\Logs\*" -Recurse -Force -Destination "C:\Logs"
Copy files from multiple folders to one Folder using PowerShell
Here’s a PowerShell script that you can use to copy multiple files from a source folder to a destination folder:
Copy-Item -Path C:\Logs\Log-1.txt,C:\temp\log-2.txt -Destination C:\LogsV2
Similarly, you can copy all files from multiple folders with PowerShell, as:
Copy-Item -Path "C:\Temp\Logs V2\*","C:\Temp\Logs V3\*" -Destination "C:\Logs"-Recurse -force
PowerShell to Include or Exclude Files to Copy
You can also use the -Include or -Exclude switch, along with wildcard characters, to filter the files you want to copy. For example, you can use -Include to copy only the files with specific extensions:
Copy-Item -Path "C:\Temp\*" -Destination "C:\Logs" -Include "*.log"
This script will copy only the files with the extension log.
You can also use -Exclude to exclude certain files or folders from the copy process:
Copy-Item -Path "C:\Temp\*" -Destination "C:\Logs" -Exclude "*.bak","*.tmp"
This script will copy all files and folders except the ones with the .bak extension and the folder named ‘temp’.
Wrapping up
In summary, the Copy-Item
cmdlet is a powerful tool for copying files in PowerShell. With the use of just a few simple commands, users can quickly and easily transfer files between folders or locations. By understanding the basic syntax and options available, you can easily copy files from one location to another. This makes PowerShell an invaluable tool for administrators who need to quickly and easily copy files with minimal effort.