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

Updated on: 12-Mar-2020

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements