• Linkedin
  • Reddit

Summary :

PowerShell copy file

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.

Quick Navigation :

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.

connect to exchange online powershell thumbnail
How to Connect to Exchange Online PowerShell? [Full Guide]

Do you know what is Exchange Online PowerShell? How to connect to Exchange Online PowerShell? This post provides you with a full guide to achieve that.

Read More

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.

select Windows PowerShell Admin

Step 2. In the elevated PowerShell window, type the following cmdlet and hit Enter.

Copy-Item "F:\UserData\My Documents\debug.log.txt" -Destination "C:\Logs"

copy a file to the specified directory with PowerShell

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

Note: If you want to include the Logs directory in the copy, you can remove the \* from the Path: 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:\Drawings\Logfiles" -Recurse

Note: If you want to copy all contents of the source directory, including the subdirectory trees, to the new directory, you can run the cmdlet: Copy-Item -Path "C:\Logs\*" -Destination "C:\Drawings\Logfiles" -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 “\\Server01\Share” directory to the “\\Server12\ScriptArchive” 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 "\\Server01\Share\Get-Widget.ps1" -Destination "\\Server12\ScriptArchive\Get-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 Contoso\User01 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 "Contoso\User01"

Copy-Item "D:\Folder001\test.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.

Note: Any files or subfolders won’t be copied to the remote computer without using the Recurse switch. This command will create the Folder002_Copy folder if it doesn’t exist.

$Session = New-PSSession -ComputerName "Server02" -Credential "Contoso\User01"

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 "Contoso\User01"

Copy-Item "C:\MyRemoteData\test.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:\MyRemoteData\scripts folder in the remote computer to the local D:\MyLocalData folder, you can run the following command.

$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\User01"

Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\" -FromSession $Session

check powershell version thumbnail
Here Is a Way to Check PowerShell Version on Windows 10/8/7

Do you know how to check PowerShell version on Windows 10/8/7? This post introduces how to find your PowerShell version on Windows PC with detailed steps.

Read More
  • Linkedin
  • Reddit

User Comments :