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
withGet-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
:
DemoFolder2
after running the command:
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:
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 FacebookRelated Article - PowerShell Copy
- Copy and Paste From a Variable in PowerShell
- PowerShell to Copy Files Over the Network
- Copy Files and Folders in PowerShell
- Copy File to Remote Computer in PowerShell