File handling is the essential operation to perform in computing devices. Some files do need a copy to ensure their presence on PC, even if the file is deleted from one location. In PowerShell, the Copy-item cmdlet enables you to copy your files or folders from one place to another while working in a PowerShell environment. We will use PowerShell to guide you to demonstrate the copy items.
At first, open your PowerShell terminal. To do so, click on search and type “PowerShell” in the search box.
Method 1: How to copy files using PowerShell
The first method we are going to demonstrate will copy a single file to the destination directory. As we are using the copy-item cmdlet, so the original file will be kept safe. Execute the command below to copy a file to the directory; copy the “C:\Survey\output.txt” file to the destination folder “C:\output”.
To verify that the file has been copied or not, execute the following command to get the details of the tree items inside the “output” directory.
In this method, we have demonstrated to copy files using PowerShell; moreover, the upcoming method will show to copy the content of directories.
Method 2: How to copy the entire directory to another directory
Before digging into this method, first, we will check how many files and directories exist in our source folder. To do that, execute the following command:
Here, you can observe that one directory exists in the “project” folder:
In our case, the command will copy files of the “project” directory to the destination “C:\output”.
To verify that files are copied or not, run the following command. At first, you will check files of the parent folder (which is “output”); after that, you must check the subfolder (which is “old files”). The “old files” is a directory inside “C:\Survey\project” which contains three .docx files.
The command given below will check for the content inside “old files”:
As you can check from the output, the command has been executed successfully but has no result, so it means that the files inside “old files” are not copied. The content of the child directories will not be copied until you use the “-Recurse” flag. This flag will copy all the content of parent and child directories.
You must add the flag “-Recurse” to copy the files inside the subfolder; the command given below will be used in this option.
Moreover, to verify that the content of the subfolder is copied or not, execute the following commands to check. The command below will show you the files and directories inside “output”.
The command below will check the content of the subdirectory “old files”: However, you can check that the output is similar to that in “Option 1”, but the content of the subfolder “old files” is copied.
Method 3: How to copy multiple folders using PowerShell
You can copy more than one source directory to another directory. So, you must execute the following command to copy multiple folders. The command below will copy all the content of three directories (“Survey”, “Survey1,” and “Survey2”) to the destination folder.
To check that the content has been copied or not; run the following command in “PowerShell”:
The command’s output shows that all the files and directories inside three directories are copied to the destination directory “newoutput”.
Method 4: Copy selective files of a directory
This method will guide you to copy specific file types from one directory to another directory. The “-Filter” flag is used to copy only particular kinds of files; you have to put the extension of filetype (.docx, .pdf) after the “-Filter” flag. The command below will copy all the MS document files from source folder “Survey” to target folder “output1”.
Moreover, to verify that the files are copied or not; execute the following command in the “PowerShell” terminal:
To check the content of “output1”; execute the following command:
Method 5: Copy other than excluded files
You can exclude specific types of files while copying. For example, you want all the files other than “.pdf”, then you have to use the “-Exclude” flag to do that. The following command will exclude all “.pdf” files from copying:
To check the output, execute the following command to ensure that any “*.pdf” file is not copied.
The result shows that the “.pdf” file is copied to the targeted folder.
Conclusion:
PowerShell is a well-known and most accessible scripting language as compared to traditional ones like VB scripts. It allows you to perform multiple operations automatically. For instance, creating files, copying files, etc.
Similarly, various operations can be performed using cmdlet of PowerShell. In this guide, we have demonstrated multiple methods to copy files and directories from source folder to target folder. Moreover, PowerShell provides different copying options, such as copy specific files, exclude few files from copying.