- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy folder contents in PowerShell with –Recurse parameter?
To copy the contents of the folder to the destination folder in PowerShell, you need to provide the source and destination path of the folder, but need to make sure that you need to use a wildcard (*) character after the source path, so the entire folder content gets copied.
If you provide only source folder without (*), only folder name gets copied without its contents. We also need to make sure both source and destination folder exists.
Example
Copy-Item -Path D:\Temp\ -Destination D:\TempContent -PassThru
Output
When you use the above command, you will see the output will be none, because there is no (*) character specified.
PS C:\WINDOWS\system32> Copy-Item -Path D:\Temp\ -Destination D:\TempContent -PassThru Directory: D:\TempContent Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 06-03-2020 10:55 Temp
Output
But when you specify the (*) character then folder content gets copied.
Example
Copy-Item -Path D:\Temp\* -Destination D:\TempContent -PassThru
Output
PS C:\WINDOWS\system32> Copy-Item -Path D:\Temp\* -Destination D:\TempContent -PassThru Directory: D:\TempContent Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 06-03-2020 10:56 GPO_backup d----- 06-03-2020 10:56 LGPO -a---- 27-01-2020 22:21 13962 Alias1 -a---- 26-01-2020 19:20 13818 aliases.txt -a---- 07-05-2018 23:00 301 cars.xml -a---- 18-01-2020 18:25 48 delim.txt -a---- 18-01-2020 17:25 14 GetContentExample.txt -a---- 29-12-2017 15:16 4526 healthcheck.htm -a---- 29-12-2017 15:16 4526 healthcheck1.htm -a---- 20-01-2020 12:10 1148809 PowerShellcommands.csv -a---- 17-01-2020 22:04 55190 Processes.txt -a---- 18-01-2020 18:22 27620 ReadC.txt -ar--- 13-01-2020 18:19 0 Readonlyfile.txt -a---- 08-12-2017 10:24 48362 servicereport.htm -a---- 08-12-2017 10:24 48362 servicereport1.htm -a---- 18-01-2020 18:44 22 stream1.txt -a---- 08-12-2017 10:16 393 style.css -a---- 12-12-2017 23:04 1034 tes.htmoutput.htm -a---- 08-12-2017 11:29 7974 Test.xlsx -a---- 25-10-2017 08:13 104 testcsv.csv -a---- 18-01-2020 16:26 27620 testreadC.txt
In the above example, you might have noticed that there are subfolders as well like LGPO and GPO_backup, but when subfolders are copied to the destination then they are empty and only the files under the parent folder get copied.
Example
To copy the subfolder contents as well, you need to use –Recurse parameter. An example is given below.
PS C:\WINDOWS\system32> Get-ChildItem D:\TempContent\LGPO\ Directory: D:\TempContent\LGPO Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 01-06-2017 03:22 410088 LGPO.exe -a---- 01-06-2017 02:25 638115 LGPO.pdf
- Related Articles
- How to view folder NTFS permission with PowerShell?
- How to get the Shared folder permissions with PowerShell?
- How to validate the path with the PowerShell function parameter?
- How to copy files with the specific extension in PowerShell?
- How to use the ErrorAction parameter in PowerShell?
- How to share a windows folder using PowerShell?
- How to remove windows folder sharing using PowerShell?
- How to get the folder size using PowerShell?
- How to copy multiple files in PowerShell using Copy-Item?
- How to copy only updated or newer files with PowerShell?
- How can I copy a file from one folder to another folder within a container in Docker?
- How to use an alias() for the parameter in PowerShell?
- How to Copy files or Folder without overwriting existing files?
- How to copy files from one folder to another using Python?
- How to use –Wait parameter in Restart-Computer cmdlet in PowerShell?
