PowerShell is a tool to run different tasks in Windows automatically; moreover, its Linux-based support is also available for multiple distributions, such as Ubuntu 16.04, Ubuntu17.04, CentOS 7, Arch Linux, and so on. It uses a command-line interface to execute cmdlet and perform various operations such as creating files and copying files and folders. In this guide, we will serve the copy operations of PowerShell.

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1-1.jpg" data-lazy- height="515" src="data:image/svg xml,” width=”344″>

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:Surveyoutput.txt” file to the destination folder “C:output”.

> Copy-item -Path “C:Surveyoutput.txt” -Destination “C:output”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2-1.jpg" data-lazy- height="70" src="data:image/svg xml,” width=”763″>

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.

> Get-ChildItem -Path “C:output”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3-1.jpg" data-lazy- height="208" src="data:image/svg xml,” width=”680″>

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:

> Get-ChildItem -Path “C:Surveyproject”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/4-1.jpg" data-lazy- height="322" src="data:image/svg xml,” width=”888″>

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”.

> Copy-item “C:Surveyproject*” -Destination “C:output”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/5-1.jpg" data-lazy- height="62" src="data:image/svg xml,” width=”696″>

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:Surveyproject” which contains three .docx files.

> Get-ChildItem -Path “C:output”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/6-1.jpg" data-lazy- height="333" src="data:image/svg xml,” width=”885″>

The command given below will check for the content inside “old files”:

> Get-ChildItem -Path “C:outputoldfiles”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/7-1.jpg" data-lazy- height="91" src="data:image/svg xml,” width=”677″>

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.

> Copy-item “C:Surveyproject*” -Destination “C:output” -Recurse

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/8-1.jpg" data-lazy- height="67" src="data:image/svg xml,” width=”784″>

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”.

> Get-ChildItem -Path “C:output”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/9-1.jpg" data-lazy- height="328" src="data:image/svg xml,” width=”887″>

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.

> Get-ChildItem -Path “C:outputoldfiles”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/10-1.jpg" data-lazy- height="237" src="data:image/svg xml,” width=”635″>

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.

> Copy-Item C:Survey*,C:Survey1*,C:Survey2* -Destination C:newoutput -Recurse

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/11.jpg" data-lazy- height="76" src="data:image/svg xml,” width=”1156″>

To check that the content has been copied or not; run the following command in “PowerShell”:

> Get-ChildItem -Path “C:newoutput”

The command’s output shows that all the files and directories inside three directories are copied to the destination directory “newoutput”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/12.jpg" data-lazy- height="299" src="data:image/svg xml,” width=”613″>

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”.

> Copy-Item -Path C:Survey* -Destination C:output1 -Filter ‘*.docx’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/13.jpg" data-lazy- height="63" src="data:image/svg xml,” width=”789″>

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:

> Get-ChildItem -Path “C:output1”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/14.jpg" data-lazy- height="271" src="data:image/svg xml,” width=”888″>

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:

> Copy-Item “C:Survey*” -Destination “C:output2” -Recurse -Exclude ‘*.pdf’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/15.jpg" data-lazy- height="89" src="data:image/svg xml,” width=”1047″>

To check the output, execute the following command to ensure that any “*.pdf” file is not copied.

> Get-ChildItem -Path “C:output2”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/16.jpg" data-lazy- height="356" src="data:image/svg xml,” width=”889″>

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.