A great many people don’t know how to copy a file using PowerShell. If you also trying to figure it out, you come to the right place. This post of MiniTool writes 8 common examples for the PowerShell copy file operation.
Introduction
The PowerShell copy-item cmdlet means that you can copy an item from one location to another location in the same namespace. When you run the PowerShell copy item cmdlet, it will overwrite the file by default if it already exists. It can copy a file to a folder, but it can’t copy a file to a certificate drive.
The copy item PowerShell cmdlet doesn’t delete or cut the files being copied. The particular items that the cmdlet can copy rely on the PowerShell provider that exposes the items. For example, it can copy files and directories in a file system drive and entries and registry keys in the registry drive.
In addition, the Powershell copy files cmdlet can copy and rename the files in the same command. To rename a file, you can enter the new name to the value of the Destination parameter. If you just want to rename an item but not copy it, you can use the Rename-Item cmdlet.
How to Copy Files with PowerShell
How to perform the PowerShell copy file cmdlet? Now, you can refer to the 8 common examples below.
Example 1. PowerShell Copy Files to a Specified Directory
The most commonly asked question for the PowerShell copy item cmdlet is how to copy a file to the specified directory. For example, we copy the debug.log.txt file to the C:Logs directory. Here’s how to do that:
Step 1. Right-click the Start menu at the bottom left corner of your screen and select Windows PowerShell (Admin) from the context menu. Then click on Yes in the UAC window to access PowerShell with the admin right.
Step 2. In the elevated PowerShell window, type the following cmdlet and hit Enter.
Copy-Item “F:UserDataMy Documentsdebug.log.txt” -Destination “C:Logs”
Example 2. PowerShell Copy Files to an Existing Directory
If you want to copy the contents of a directory into an existing directory, you can refer to this guide. For example, we copy the contents of the C:Logs directory into the existing C:Drawing directory. The Logs directory isn’t copied. Assume the Logs directory contains files in subdirectories, then the subdirectories will be copied with their file trees. The Container parameter is set to True by default, which preserves the directory structure.
Copy-Item -Path “C:Logs*” -Destination “C:Drawings” -Recurse
Example 3. PowerShell Copy File to a New Directory
Sometimes you may want to copy the contents of a directory to a new destination directory. In this example, we copy the contents of the C:Logs source directory and create a new directory “Logfiles” in the C:Drawings directory.
To contain the source directory’s name, you need to copy to an existing destination directory as shown in Example 2. Or, you can rename the new directory as the same as the source directory.
Copy-Item -Path “C:Logs” -Destination “C:DrawingsLogfiles” -Recurse
Example 4. PowerShell Copy File to a Specified Directory and Rename It
In this example, we show you how to copy a file to the specified directory and rename the file. To copy the Get-Widget.ps1 file from the “\Server01Share” directory to the “\Server12ScriptArchive” directory, you can run the following PowerShell Copy-Item cmdlet, which will change the file name from Get-Widget.ps1 to Get-Widget.ps1.txt.
Copy-Item “\Server01ShareGet-Widget.ps1” -Destination “\Server12ScriptArchiveGet-Widget.ps1.txt”
Example 5. PowerShell Copy File to a Remote Computer
In this example, we show you how to copy a file to a remote computer. Here you can create a session to the remote computer named Server01 with the credential of ContosoUser01 and store the results in the variable named $Session.
To copy the test.log file from the D:Folder001 folder to the C:Folder001_Copy folder on the remote computer using the session information stored in the $Session variable, you can run the command below in PowerShell.
$Session = New-PSSession -ComputerName “Server01” -Credential “ContosoUser01”
Copy-Item “D:Folder001test.log” -Destination “C:Folder001_Copy” -ToSession $Session
Example 6. PowerShell Copy Folder to a Remote Computer
This example shows how to make PowerShell copy folder to a remote computer. To copy the D:Folder002 folder to the C:Folder002_Copy directory on the remote computer, you can run the command below.
$Session = New-PSSession -ComputerName “Server02” -Credential “ContosoUser01”
Copy-Item “D:Folder002” -Destination “C:Folder002_Copy” -ToSession $Session
Example 7. Copy a Remote File to the Local Computer
You can also copy a remote file to the local computer named Server01 using PowerShell. For example, to copy the test.log file from the remote directory “C:MyRemoteData” to the local directory “D:MyLocalData”, you can follow the command below.
$Session = New-PSSession -ComputerName “Server01” -Credential “ContosoUser01”
Copy-Item “C:MyRemoteDatatest.log” -Destination “D:MyLocalData” -FromSession $Session
Example 8. Copy the Contents of a Remote Folder to the Local Computer
Here we show you how to copy the entire contents of a folder from a remote computer to the local computer. For example, to copy the contents of the C:MyRemoteDatascripts folder in the remote computer to the local D:MyLocalData folder, you can run the following command.
$Session = New-PSSession -ComputerName “Server01” -Credential “ContosoUser01”
Copy-Item “C:MyRemoteDatascripts” -Destination “D:MyLocalData” -FromSession $Session