| by Arround The Web | No comments

Copy-Item: Copy Files Like a Boss in PowerShell

Windows users prefer copying files or folders using GUI(Graphical User Interface). However, PowerShell also offers the functionality of copying files and folders. The “Copy-Item” command in PowerShell is used to copy one item from one location to another with the same name. It does not cut or delete the item after being copied. Moreover, it cannot copy the file to a certificate driver but can copy it to a folder.

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:

> Copy-item "C:\Doc\File.txt" -Destination "C:\New"

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:

> Copy-item "C:\Doc\File.txt","C:\Doc\New.txt" -Destination "C:\New"

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:

> Copy-item "C:\Doc\*" -Destination "C:\New" -recurse

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:

> Copy-item "C:\Doc" -Destination "C:\Files" -recurse

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:

> Copy-item "C:\Doc\*" -Destination "C:\New" -recurse

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.

Share Button

Source: linuxhint.com

Leave a Reply