How to use PowerShell to Copy a Folder?

Requirement: Copy a folder using PowerShell.

How to Copy a Folder using PowerShell?

Copying folders in PowerShell is a common requirement for many IT professionals and system administrators. It allows them to create backups, migrate data, or simply organize files. 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.

Understanding the Basics of PowerShell Copy Folder

Before diving into the details of how to use the PowerShell copy folder, it is important to understand the basic syntax of the cmdlet. 

The syntax of the PowerShell copy-Item cmdlet is as follows:

Copy-Item 
[-Path] <String[]> 
[[-Destination] <String>] 
[-Container] 
[-Force] 
[-Recurse] 
[-Filter <String>] 
[-Include <String[]>] 
[-Exclude <String[]>] 
[-PassThru] 
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]

Here is the list of important parameters in the Copy-Item cmdlet.

ParameterDescription
-PathThe path parameter specifies the location of the folder or file that you want to copy
-DestinationThe destination parameter specifies the location where you want to copy the folder
-ContainerSwitch to copy the folder structure but not the files within it
-ForceOverwrites the destination file or folder if it already exists
-RecurseCopies all files and folders in the specified folder and its subfolders

Example: PowerShell to Copy a Folder

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 “Logs” from the “C:\Temp” directory to the “C:\Backup” directory, the command would look like this:

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

The -Recurse parameter tells PowerShell to include all sub folders. The Copy-Item cmdlet automatically creates the destination folder (You don’t have to pre-create the destination Folder using the New-Item cmdlet). 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.”

PowerShell Copy File Overwrite

By default, this cmdlet doesn’t overwrite the contents. 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 PowerShell script:

Copy-Item -Path "C:\Users\Username\Desktop\Example Folder" -Destination "D:\Backup\New Folder" -Recurse

In the above command, the folder and its contents will be copied to the “D:\Backup” directory, but the copied folder will be named with a new name, “New Folder”, instead of “Example Folder”.

Copy Specific Files from a Folder

Now, let’s say you only want to copy certain files or subfolders within the source folder. You can use wildcards to specify the files or folders you want to copy. For example, if you only want to copy all PNG files in the source folder:

Copy-Item "C:\Temp\*.log" -Destination "C:\Temp\Logs"

This will copy all LOG files in the source folder (C:\Temp) to the destination folder (C:\Temp\Logs), but no other files or subfolders.

Another useful parameter is -Exclude, which allows you to exclude certain files or subfolders from the copy using wildcard characters. For example, if you want to copy all files in the source folder except for file types “*.txt”:

Copy-Item "C:\Temp\Logs\*.*" -Destination "C:\Temp\Logs\Archive" -Exclude "*.txt"

This will copy all files in the source folder (C:\Temp\Logs) except for any text files to the destination folder (C:\Temp\Logs\Archive). Make sure the destination folder exists already.

Copy Folder Structure in PowerShell

If you want to copy a folder’s structure (meaning the directories and subdirectories, but not the files), you can use the “Get-ChildItem” and “Copy-Item” cmdlets in PowerShell. Here’s an example of how to do this:

#Parameters
$source = "C:\Temp"
$destination = "C:\Logs"

#Copy Each Folder from source to the destination
Get-ChildItem -Path $Source -Recurse | Where-Object { $_.PSIsContainer } | ForEach-Object {
    $dest = Join-Path $destination $_.FullName.Substring($Source.length);
    New-Item -ItemType Directory -Path $dest -Force;
}

This script will create a mirror of the source directory’s structure in the destination directory, excluding any files.

Copying All Files from One Folder to Another Using PowerShell

Sometimes, you may only want to copy all the files from one folder to another without copying the subfolders. PowerShell provides a simple way to achieve this using the “Copy-Item” cmdlet. Here’s an example:

Copy-Item -Path "C:\Temp\*.*"  -Destination "C:\Logs" -Force

In this example, the “Copy-Item” cmdlet is used to copy all the files from the “Temp” located at “C:\” to the “Logs” located at “C:\”.

Copy Directory to Remote Computer using PowerShell

It’s also worth noting that the “Copy-Item” command has the ability to copy folders to a remote computer. 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” from the local computer file system 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"

Tips for Copying Folders in PowerShell

To ensure efficient folder copying in PowerShell, here are a few tips and tricks to make copying folders in PowerShell easier:

  • Include error handling to handle any potential issues during the copying process.
  • 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.
  • Test the copy operation on a small subset of files before performing it on a large-scale to ensure accuracy.
  • Consider using the “-Filter” parameter to copy specific files based on file extensions or other criteria. We also have “-Include” and “-Exclude” parameters.
  • Check the source and destination paths to ensure they are correct and accessible.
  • Verify that you have the necessary permissions to copy folders and files.

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.

How to copy a folder from one directory to another using PowerShell?

To copy a folder from one directory to another using PowerShell, you can use the Copy-Item cmdlet. Here is an example of the command you can use:
Copy-Item -Path "C:\AppLogs\Logs" -Destination "C:\Archive\Logs" -Recurse
This script will copy the Folder “Logs” from the source directory “C:\AppLogs” to the destination directory “Logs” in “C:\Archive”.

How do I copy the latest file from a folder in PowerShell?

To copy the latest file from a folder using PowerShell, you can use the Get-ChildItem cmdlet to retrieve the files sorted by their LastWriteTime property. Then, you can use the Copy-Item cmdlet to copy the files to your desired destination. Here is an example of the PowerShell script you can use:
#Get the latest file
$LatestFile = Get-ChildItem -Path "C:\Temp\Logs" -File | Sort-Object LastWriteTime -Descending | Select-Object -First 1
#Copy the latest file
Copy-Item -Path $LatestFile.FullName -Destination "C:\Temp\NewLogs\$($LatestFile.Name)"

How do I move a Folder from one directory to another in PowerShell?

If you want to move a folder from one directory to another in PowerShell, you can use the Move-Item cmdlet.
Move-Item -Path "C:\Temp\Logs" -Destination "C:\Temp\Archive\Logs"
Make sure the Destination Folder “C:\Temp\Archive” already exists prior to moving.

How to copy multiple files from one directory to another in PowerShell?

To copy multiple files from one directory to another in PowerShell, you can use the Copy-Item cmdlet. Here’s an example of the command you can use:
Copy-Item -Path "C:\Temp\Logs\*" -Destination "C:\Temp\Logs-Archive" -Recurse -Force

How do I merge files in multiple folders?

If you want to merge files from multiple folders into a single folder (without merging the contents of the files), you can use the Copy-Item cmdlet in PowerShell. Here’s a simple script that does this:
Copy-Item -Path C:\Temp\Logs\*,C:\Temp\Logs-Archive\* -Destination C:\Temp\AllLogs

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “How to use PowerShell to Copy a Folder?

  • I have a main folder c:\folder1

    there are sub folders in path d:\folder2
    sub folder – d:\folder2 \subfolder1\file1.txt
    d:\folder2 \subfolder2\file2.txt

    how to move file1 and file2 to c:\folder1

    Reply
  • My paths have spaces and I receive error: Copy-Item : A positional parameter cannot be found that accepts argument

    Reply
  • Your example is using wrong dirs

    Reply

Leave a Reply

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