The Copy-Item cmdlet allows PowerShell copy folder and subfolders commands. Under different scenarios, we use Copy-Item commands and parameters to copy files from source to destination. The post talks about PowerShell commands to execute under various requirements.
Folders help you segregate files in an organized manner for easy access. If you want to copy or move files, folders allow you to do so with minimal time. As copying is a common requirement for many people, Windows's built-in tool, PowerShell, makes it easy. With its copying commands, the powerful scripting tool can copy folders and subfolders on the computer. The guide from EaseUS talks about using Powershell copy folder and subfolder commands to copy folders from one place to another.
PowerShell is a command line tool and scripting language. It is designed for system administration, and its commands are called cmdlets. PowerShell allows you to manage computers with CL commands and completes complex tasks and expression parsing using a fully developed scripting language. Copy-item is one of the cmdlets in PowerShell that help you copy files, folders, and registry keys, and it is available from one destination to another without any external app.
The Copy-item command uses the source and destination path parameters to copy the files. The command gives an error if a file with a similar name exists. Many command shells cut or delete the items from the source once you copy them, but that's not the case with PowerShell. A generic Copy-item path command to copy a single file is written as: Copy-Item -Path "Sourcepath" -Destination "Destination"
In various situations, we need to make minute changes to the Copy-item command. We will also add parameters like Force, Recurse, Container, etc., to get the desired results. Let us examine five everyday situations when copying folders using PowerShell. If you want an automatic way to copy files, you can skip to Part 3.
We can use the Copy-Item with Recurse parameter to copy a folder and all its content, including the subfolders. The Recurse parameter indicates that the PowerShell should include all the subdirectories. But if the destination folder already consists of the same folder or file, PowerShell gives an error saying, "An item with a specified name already exists." Since Copy-Item does not overwrite by default, we add the Force parameter to handle the case.
Copy-Item -path "C:\Temp\ExampleFolder" -Destination "D:\Backup" -Recurse -Force
The example copies a folder named "ExampleFolder to the Backup directory in D Drive. Even if a folder named ExampleFolder exists, it will be overwritten.
One reason we use the folder concept is to organize files. You can use wildcards to complete the task if you want to extract or copy specific types of files or subfolders from the source folders.
Copy-Item "C:\Temp\*.txt" -Destination "C:\Temp\TextFiles"
This will copy all the text files from the Temp directory to the Text files directory on the same C Drive.
Similarly, you can use the Exclude parameter to exclude specific files from the folder. It will copy everything except the file types specified with Exclue.
Copy-Item "C:\Temp\Logs\*.*" -Destination "C:\Temp\Logs\Images" -Exclude "*.txt"
In some cases, you may want to copy the folder's structure (tree structure of the directories and subdirectories), the "Get-ChildItem" and "Copy-Item" cmdlets will be helpful.
Here's an example of using Get-ChildItem to copy the folder structure in PowerShell.
If the requirement is to copy all the files present in the folders, excluding the subfolder, you can use the command below:
Copy-Item -Path "C:\Temp\*.*" -Destination "C:\Logs" -Force
The command will copy all the files from the Temp directory of C Drive to the Logs folder of C Drive.
As discussed above, PowerShell is very powerful. The tool allows you to copy folders to a remote computer using the Copy-Item. To achieve this, you must include the destination directory UNC path on the remote PC. Let's say you want to copy the ExampleFolder in the Temp directory to a remote computer named RemotePC to the "D:\Backup" folder.
Copy-Item -Path "C:\Temp\ExampleFolder" -Destination "\\RemotePC\D$\Backup"
Share this article on your social media to help others find a way to copy files and folders!
While dealing with data, there can be data loss situations when transferring or copying files. Many command line tools delete the files in the source folder when they try to copy to the destination. Under such circumstances, you may lose data. Accidental deletion of data is common with command line tools when performing copy operations. It is highly advisable to back up files or folders instead of copying them.
A professional tool like EaseUS Todo Backup will secure your data and help you restore it in accidents. With a few clicks, EaseUS will create backups and offer flexible storage options, be it a file, folder, hard drive, or system.
It can help you perform full, incremental, and differential backups and store them on the PC, external drive, or the cloud. It lets you create an ISO image of a USB drive and automatically back up your files for data security. With advanced algorithms and features, you can achieve backup and restoration efficiently. As the best file sync software for Windows, it can also sync your files to different locations. Download EaseUS Todo Backup to protect your data.
Step 1. To initiate a backup, click the Select backup contents option.
Step 2. There are four data backup categories, File, Disk, OS, and Mail, click File.
Step 3. On the left, both local and network files will be shown. You may extend the directory to pick the backup files.
Step 4. Follow the on-screen instructions and choose a location to store the backup.
Click Options to encrypt the backup with a password in Backup Options, then click Backup Scheme to specify the backup schedule and start the backup at a certain event. There are also other additional choices you may choose to tailor the backup process to your requirements.
Step 5. The backup may be stored on a local disk, EaseUS' online service, or NAS.
Step 6. EaseUS Todo Backup enables customers to back up data to a third-party cloud drive as well as its own cloud drive.
If you want to back up data to a cloud drive provided by a third party, choose Local Drive, scroll down to add Cloud Device, and then add and log in to your account.
You can also choose the EaseUS cloud drive. Sign up for an EaseUS Account, then click EaseUS Cloud and log in.
Step 7. Click Backup Now to begin the backup process. The backup job will be presented as a card on the left side of the panel after it has been finished. Right-click the backup job to handle it further.
Copying folders from one location to another manually is a complex and time-consuming task. PowerShell copy folder and subfolders cmdlets allow you to swiftly copy the directory structures, files, and folders. The post details the commands and parameters one needs to use with PowerShell's Copy-Item to copy folders and subfolders.
Command-line tools may be error-prone if you execute the wrong commands. Accidental deletion of data due to mistakes will result in data loss. Safely back up all your files and folders to an external drive or cloud using EaseUS Todo Backup for instant recovery.
Here are some frequently asked questions on Copy folder and subfolders PowerShell.
1. How to copy a directory structure without copying files?
XCopy is a robocopy command that copies files from a source to a destination on a Windows PC. To copy a directory structure without files, you must add two XCopy switches, /t and /e, to duplicate the directory structure without copying files.
2. How to get all files from folder and subfolders in PowerShell?
To get all files from a folder and subfolder, PowerShell offers a "Get-ChildItem" cmdlet. Get-ChildItem copies all files and folders from the source drive to the destination. Additionally, you will have to include a file parameter.
3. How can I copy files with specific names into different folders?
The Astrick (*) symbols help you copy files with specific names or types to different folders. For example, if you want to move text files from the Temp directory to the Source folder, use "C:\Temp\*.txt" as the Source path in PowerShell Copy-Item.
Related Articles
How to Backup/Export and Restore Chrome History and Cookies
How to Move Files from Subfolders to Main Folder in Windows
How to Update Acer BIOS with No Data Loss
All You Need to Know About Windows 11 Backup and Restore (Windows 7)