The Copy-Item cmdlet is used to copy an item from one path to another by using the terminal support of PowerShell. This command is used for multiple purposes such as copying a file/folder, or multiple files/folders.
In this informative guide, you will learn to apply the Copy-Item cmdlet in PowerShell.
How to use Copy-Item in PowerShell?
The copy-item cmdlet is used to copy one item from one path source location to another destination path in the same namespace. The item could be a single/multiple files, folders, sub-folders, and so on.
The working of the Copy-Item cmdlet is explained in the upcoming examples:
How to copy a single file in PowerShell using Copy-Item?
Copying a single file is the basic use of the Copy-Item cmdlet in PowerShell. The following command is to copy a file from the “C:\Work\File.txt” path to the “C:\Output” folder.
Now, let’s verify whether the file is copied or not by running the Get-Childitem cmdlet on the “C:\Output” file.
The above screenshot verifies that the file is copied from the source path to the destination path successfully.
How to copy multiple folders in PowerShell using copy-item?
Copy-item cmdlet also allows you to copy multiple folders or files altogether. It saves your important time while copying. The command written below copies the items from the “C:\File\” and “C:\Docs” to “C:\abc”.
Now, let’s verify the copying process by executing the “Get-ChildItem” cmdlet on the C:\abc\.
The above process verifies that the files are copied into the “C:abc” directory.
How to copy specific files from a folder using copy-item?
Sometimes we need to copy only selected files. For this purpose, the (*) asterisk sign is used with the file names to select those files. Let’s practice this example by getting the content of the file where we will apply the Copy-Item cmdlet:
The output shows that three files are present in the “C:\Test” directory.
Now, the following command copies the “docs.txt” file from the “C:\Test” directory to the “C:\abc\” directory.
The result of the command can be verified by the following Get-ChildItem cmdlet:
The output shows that the “docs.txt” file is copied into the “C:\abc\”.
How to copy files other than excluded using Copy-Item?
Copy-Item can be used to copy all the files except the one you have excluded. To do so, PowerShell’s “-Exclude” parameter is used with the “Copy-Item” cmdlet. For instance, there is a folder that contains the “txt” and “PDF” files. The following command will copy the items of “C:\Test\*” to the “C:\abc\” directory excluding the “.txt” files.
Let’s verify the copied files:
The output shows that only “.pdf” files are copied and the “-Exclude” flag has excluded the files other files of the source directory.
How to overwrite files and folders in PowerShell using copy-item?
Sometimes we copy files/folders from one location to another and want to overwrite the existing files. In such a case, the “Copy-Item” is applied with the “-Force” flag to replace the folder.
The following command will copy the content of the “C:\Test\*” directory to the “C:\abc\” and will overwrite the destination folder.
Let’s verify the process whether or not it was successful. We need to run the following command.
The above image shows that the folder was successfully overwritten in the destination folder.
Congratulations! You probably have learned the basic and advanced level functionality of PowerShell’s Copy-Item.
The Copy-Item is a cmdlet used to copy files from a specific path to the other location. It supports tens of flags to perform a specific copying operation on files/folders. In this guide, the functionality of the Copy-Item cmdlet is described in detail with the help of suitable examples. The Copy-Item cmdlet is enriched with a set of parameters to perform all basic copying operations. You have learned to apply the Copy-Item cmdlet in various copying scenarios.