Copy-Item Excluding Folder in PowerShell

Sheeraz Gul Nov 15, 2022
Copy-Item Excluding Folder in PowerShell

This tutorial demonstrates how to copy-item excluding folder in PowerShell.

PowerShell Copy-Item Exclude Folder

We can use the Copy-Item command to copy the files while excluding a folder in PowerShell. There are two methods to use Copy-Item while excluding a folder, one is excluding a single folder, and the other is excluding multiple folders.

To exclude a single folder while using copy-item, use the following steps.

  • First, we use Copy-Item in the command.
  • Then, we use the Get-Item Command with -Path to get the files to be copied.
  • We also use the -Exclude with Get-Item to exclude a particular folder.
  • Then, we use the -Destination to assign the destination path.
  • Finally, we use the -Recurse and -Force to force the copy items.

All the above steps are implemented in one command. Run the following command to copy items excluding a folder:

Copy-Item -Path (Get-Item -Path "C:\Users\Sheeraz\DemoFolder1\*" -Exclude ('SubFolder')).FullName -Destination "C:\Users\Sheeraz\DemoFolder2" -Recurse -Force

The above command will copy the files of DemoFolder1 to the DemoFolder2, excluding the subfolder in the DemoFolder1. See the structure:

DemoFolder1:

CopyItem Exclude SubFolder

DemoFolder2 after running the command:

CopyItem Exclude SubFolder Output

The above command will only exclude one given folder. We can also exclude multiple folders by just putting the multiple folder names in step 3.

See the command for excluding multiple folders:

Copy-Item -Path (Get-Item -Path 'C:\Users\Sheeraz\DemoFolder1\*' -Exclude ('SubFolder', 'SubFolder1')).FullName -Destination 'C:\Users\Sheeraz\DemoFolder2' -Recurse -Force

Now this command will exclude multiple folders. We have to put multiple names in the exclude method.

See the output:

CopyItem Exclude Multiple Folders

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PowerShell Copy