This write-up will discuss about the usage of the “Copy-Item” command in PowerShell.
How to Copy Files Like a Boss in PowerShell Using the “Copy-Item” Command?
Files can be copied in PowerShell securely and safely using the “Copy-item” cmdlet. Below we have provided several examples to demonstrate the working of the “Copy-Item” cmdlet.
Example 1: Copying a Single File to Another Folder
Check out the given command:
According to the above code:
- First, add the “Copy-item” command, and specify the address from where you need to copy the file.
- Then, add the “-Destination” parameter and add the target path:
Example 2: Copying Multiple Files to Another Directory
This example will demonstrate a method to copy more than one files from one folder to another:
In the above command, we have separated the multiple files path with a comma:
Example 3: Copying Contents of a Directory to Another Directory
Now, execute the following command:
In this stated code:
- At the end of the path address, the wild character “*” is used at the end to copy the content within the folder.
- “-recurse” parameter is used to copy the sub-directories into another folder:
Example 4: Copying a Folder to Another Directory
Here is another example of copying a folder to another directory:
The “-recurse” parameter is used here to copy the sub-folders to another location:
Example 5: Create a Directory and Copy Contents of a Folder Inside it
A new directory can be created and the content can be copied inside it:
Here, the “-recurse” parameter will help to create a new directory if it does not exist already:
That was all copying files like a boss in PowerShell.
Conclusion
The “Copy-item” cmdlet’s core purpose is to copy the files or directories from one specified location to another. It copies the file to another location with the same namespace. Moreover, it does not cut or delete the file being copied. This write-up has demonstrated several scenarios for copying files in PowerShell.