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-Itemin the command.
Then, we use the
-Pathto get the files to be copied.
We also use the
Get-Itemto exclude a particular folder.
Then, we use the
-Destinationto assign the destination path.
Finally, we use the
-Forceto 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:
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
See the output:
Related 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