DEV Community

Cover image for PowerShell Copy-Item Reports vs Reports\*: The Real Difference (and Why Your Backup Ends Up Nested)
arnostorg
arnostorg

Posted on Edited on

PowerShell Copy-Item Reports vs Reports\*: The Real Difference (and Why Your Backup Ends Up Nested)

Ever run a backup script twice and found Backup\Reports\Reports sitting inside your backup? That isn't a bug in your script. It's how Copy-Item -Recurse decides where a folder lands, and the answer depends on one thing: does the destination already exist?

This tip shows the exact difference between copying a folder (Reports) and copying its contents (Reports\*), why the same command gives two different results on run one and run two, and a three-line pattern that gives you the same tree every time.

The setup

A small folder to copy:

New-Item -ItemType Directory -Path .\Reports\2026 -Force | Out-Null
'q1' | Set-Content -Path .\Reports\2026\q1.csv
'readme' | Set-Content -Path .\Reports\readme.txt
Enter fullscreen mode Exit fullscreen mode

So Reports holds readme.txt and a subfolder 2026 with q1.csv.

Copy the folder: -Path Reports

Run this when Backup does not exist yet:

Copy-Item -Path .\Reports -Destination .\Backup -Recurse
Get-ChildItem -Path .\Backup -Recurse -Name
Enter fullscreen mode Exit fullscreen mode

Output:

2026
readme.txt
2026\q1.csv
Enter fullscreen mode Exit fullscreen mode

PowerShell created Backup and treated it as the new name of the folder. That looks perfect, so the script goes into a scheduled task.

Now run the exact same line again. Backup exists this time:

Copy-Item -Path .\Reports -Destination .\Backup -Recurse
Get-ChildItem -Path .\Backup -Recurse -Name
Enter fullscreen mode Exit fullscreen mode

Output:

2026
Reports
readme.txt
2026\q1.csv
Reports\2026
Reports\readme.txt
Reports\2026\q1.csv
Enter fullscreen mode Exit fullscreen mode

When the destination is an existing folder, PowerShell copies the source folder into it. Same command, different tree. That is where the "nested Reports" comes from.

The rule:

Destination Copy-Item -Path Reports -Destination Backup -Recurse
Backup does not exist Backup becomes a copy of Reports
Backup already exists You get Backup\Reports\...

Copy the contents: -Path Reports\*

The wildcard means "everything inside Reports", not the folder itself. Copy into a folder that already exists, and the result does not change between runs:

New-Item -ItemType Directory -Path .\Backup2 -Force | Out-Null
Copy-Item -Path .\Reports\* -Destination .\Backup2 -Recurse
Copy-Item -Path .\Reports\* -Destination .\Backup2 -Recurse -Force
Get-ChildItem -Path .\Backup2 -Recurse -Name
Enter fullscreen mode Exit fullscreen mode

Output, after two runs:

2026
readme.txt
2026\q1.csv
Enter fullscreen mode Exit fullscreen mode

-Force lets the second run overwrite files that are already there.

The trap inside the fix

Skip the New-Item step and the contents form gets weird. When I copied Reports\* to a Backup3 that did not exist, the first item (the 2026 folder) became Backup3, and the rest landed next to its files:

q1.csv
readme.txt
Enter fullscreen mode Exit fullscreen mode

The 2026 level is gone. So the contents form only works reliably when the destination folder exists first.

Forgetting -Recurse

Without -Recurse, copying a folder gives you the folder and nothing in it:

Copy-Item -Path .\Reports -Destination .\NoRec
(Get-ChildItem -Path .\NoRec -Recurse | Measure-Object).Count
Enter fullscreen mode Exit fullscreen mode

Output:

0
Enter fullscreen mode Exit fullscreen mode

No error, no warning, just an empty folder. If a copy "worked" but the backup is empty, check for this first.

The pattern that gives the same tree every time

Create the destination yourself, then copy the contents into it:

$source = 'C:\Data\Reports'
$dest   = 'D:\Backup\Reports'

New-Item -ItemType Directory -Path $dest -Force | Out-Null
Copy-Item -Path (Join-Path -Path $source -ChildPath '*') -Destination $dest -Recurse -Force
Enter fullscreen mode Exit fullscreen mode
  • New-Item -Force creates the folder or does nothing if it already exists, with no error.
  • Join-Path ... '*' copies what is inside Reports, never Reports itself.
  • -Force overwrites older copies on later runs.

Run it once or fifty times and D:\Backup\Reports looks the same. (It does not delete files you removed from the source. For a true mirror, use Robocopy with /MIR, carefully.)

Preview before you copy: -WhatIf

-WhatIf prints where the folder would really land, so you catch the nesting before it happens:

Copy-Item -Path .\Reports -Destination .\Backup -Recurse -WhatIf
Enter fullscreen mode Exit fullscreen mode

Output when Backup already exists:

What if: Performing the operation "Copy Directory" on target "Item: C:\Work\Reports Destination: C:\Work\Backup\Reports".
Enter fullscreen mode Exit fullscreen mode

See Backup\Reports in the destination? That's the nested copy, caught before it happened.

Quick reference

You want Use
A one-time copy to a new folder name Copy-Item -Path .\Reports -Destination .\Backup -Recurse
A repeatable backup with the same tree every run New-Item -ItemType Directory -Path $dest -Force then Copy-Item -Path "$source\*" -Destination $dest -Recurse -Force
The folder itself inside an existing folder Copy-Item -Path .\Reports -Destination .\Archive -Recurse (Archive exists)
To check first Add -WhatIf

I tested every block above in PowerShell 7.4.6. The demo ran on Linux with forward slashes; the behavior is the same with Windows paths. Docs: Copy-Item on Microsoft Learn, New-Item, and about_Wildcards.

Related tips: Copy-Item basics for duplicating files, Test-Path checks before you act, and Join-Path without slash bugs.

Practice it hands-on

Folder-copy rules stick faster when you break them yourself. CMD Master's free interactive PowerShell, CMD and Bash terminal lessons run in your browser, so you can practice file and folder commands without risking a real backup drive.

Your turn: what does this output?

Archive already exists and is empty. What does the last line print?

Copy-Item -Path .\Reports -Destination .\Archive\Reports -Recurse
Copy-Item -Path .\Reports -Destination .\Archive\Reports -Recurse
(Get-ChildItem -Path .\Archive -Recurse -Directory).Count
Enter fullscreen mode Exit fullscreen mode

Drop your number in the comments before you run it. I'll reply with the answer and why.

Top comments (0)