How to use PowerShell to Copy a Folder?

Requirement: Copy a folder using PowerShell.

How to Copy a Folder using PowerShell?

Working with files and folders from GUI can be tedious when you have to manage folders repeatedly. Luckily, We have PowerShell! PowerShell is a powerful scripting tool that allows you to automate various tasks, including copying folders. This guide will go over the steps to copy a folder using PowerShell, including how to copy a folder and subfolders, how to copy all files from one folder to another, etc. Whether you’re a beginner or an experienced PowerShell user, this guide provides the knowledge you need to copy folders quickly and easily.

To copy a folder and its contents using PowerShell, you can use the Copy-Item cmdlet with the -Recurse parameter. This cmdlet will allow you to copy any file or folder from one location to another. Here’s the basic syntax:

Copy-Item -Path "SourcePath" -Destination "DestinationPath" -Recurse

Here, “SourcePath” is the path to the folder you want to copy, and “DestinationPath” is the path you would like to copy. For example, if you’re going to copy a folder named “ExampleFolder” from the “C:\Users\Username\Desktop” directory to the “D:\Backup” directory, the command would look like this:

Copy-Item -Path "C:\Temp\Logs" -Destination "C:\Backup" -Recurse

Here is the script output in verbose mode:

powershell copy folder

This cmdlet creates the destination folder (“C:\Backup” in this case) if it doesn’t exist already. It is also possible to specify a specific file type to copy, such as “-Include *.txt,” to only copy files with a .txt extension.

PowerShell to Copy a Folder and contents – Overwrite Existing

When you copy a folder using the above methods, the folder and all its contents will be copied to the destination. If the destination folder already contains a folder or file with the same name, You’ll see an error message: “Copy-Item : An item with the specified name <Path> already exists.”

So, to overwrite the files and folders in the destination, use the -Force parameter.

Copy-Item -path "C:\Users\Username\Desktop\ExampleFolder" -Destination "D:\Backup" -Recurse -Force

This script will copy every file and subfolder in the source folder into the destination folder. Additionally, if there are any existing files in the destination folder that have the same name as those in the source folder, they will be overwritten without prompting for confirmation (unless you specify the “-confirm” switch otherwise).

Copy a Folder and rename it in PowerShell

If you want to copy the folder and its contents but want to give the copied folder a different name, you can use the following command:

Copy-Item -Path "C:\Users\Username\Desktop\ExampleFolder" -Destination "D:\Backup\NewFolderName" -Recurse

In this example, the folder and its contents will be copied to the “D:\Backup” directory, but the copied folder will be named “NewFolderName” instead of “ExampleFolder”.

Copy a folder to Remote Computer using PowerShell

It’s also worth noting that both the “Copy-Item” command have the ability to copy folders to a remote computer, in order to do that, you need to specify the UNC path of the destination folder. For example, if you want to copy the “ExampleFolder” from “C:\Users\Username\Desktop” to a remote computer named “RemotePC” in the “D:\Backup” folder, the command would look like this:

Copy-Item -Path "C:\Users\Username\Desktop\ExampleFolder" -Destination "\\RemotePC\D$\Backup"

Here are a few tips and tricks to make copying folders in PowerShell easier:

  • Use the -WhatIf parameter to see what would happen if you ran the command without actually executing it. This is useful for testing purposes.
  • Use the -Force parameter to overwrite existing files in the destination folder.
  • Use the -Recurse parameter to copy subfolders and their contents.
  • Use the -Verbose switch to display detailed information about the operation performed by the command.

Wrapping up

In this guide, we’ve covered the basics of copying folders using PowerShell. Using PowerShell to copy a folder is an easy and efficient way to copy files and folders from one location to another. By using the “Copy-Item” command and the various parameters, you can easily copy folders and their contents to any location on your computer or to a remote computer. With the ability to copy folders with different names, to overwrite the files in the destination, and to copy subfolders and files, you’ll have all the tools you need to automate your file management tasks, such as backup files and folders.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *