copy subfolders with specific names to same structure on different server


I try to copy subfolders with specific name formats to another server while maintaining the folder structure. I think I’m almost there, but it keeps copying the folders to the literal -destination root path, and I want the folders to be copied to the subfolders they were already under. The structure is already in place, I just want to replace the content in the destination with the copied content.

Source folders: any subfolder and their content within \source\folder$\folders\ where the name ends with 1*A (where * is a random character)

Destination: the exact same location, except on the \destination server. So that would be \destination\folder$\folders\

So for example, the folder and it content from \source\folder$\folders\123456\123456_17A should be copied to \destination\folder$\folders\123456\123456_17A


So far I have this:

$DestBase = '\\destination\folder$\folders'
Get-ChildItem -Path \\source\folder$\folders -Include *A -Directory -Recurse -Force | 
Copy-Item -Destination {$($_.Fullname) -replace '\\source\folder$\folders\\',"$DestBase"} -Recurse -Force -Whatif


The error that i get:

“Copy-Item : Cannot overwrite the item \source\folder$\folders\123456\123456_17A with itself.”

What am i doing wrong?

Thank in advance,



Hi Djurre,

Try this…


$SourcePath = '\\Share\Folders\'
$DestPath = '\\Server01\Folder\'
$ListDirectories = Get-ChildItem -Path $SourcePath -Recurse -Directory -Filter "*A"
foreach ($Directory in $ListDirectories)
New-Item-Path $DestDir-ItemType Directory |Out-Null

Hi Kiran,

Thanks for the reply. If I execute, I get the following error:

New-Item : A positional parameter cannot be found that accepts argument 'Directory'.
At line:7 char:1
+ New-Item -Path $DestDir-ItemType Directory |Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

I tried to Google it, but hit a wall :(.



there is a space required

$DestDir-ItemType Directory

$DestDir -ItemType Directory

Yes, as @kvprasoon mentioned, it needs space.

I don’t why syntax highlighting is not properly formatting.


$SourcePath = ‘\Share\Folders’
$DestPath = ‘\Server01\Folder’
$ListDirectories = Get-ChildItem -Path $SourcePath -Recurse -Directory -Filter “*A”
foreach ($Directory in $ListDirectories)
$DestDir = $DestPath + $Directory.FullName.ToString().Replace($SourcePath,"")
New-Item -Path $DestDir -ItemType Directory |Out-Null


Happy newyear everybody and thanks for the replies. It now returns that the path already exists, however I found another solution:

I generated a list with all the foldernames. In Excel I used a jointext function to make individual powershell commands like:

copy-item "\\source\folder$\folders\104507\104507_15A\" -destination "\\destination\folder$\folders\104507\" -recurse -force

When I executed all the lines in sequence, I got the desired result.

An admin can close this case, thanks to everybody who helped,

Greetings, Djurre


<menu id=“fcltHTML5Menu1” type=“context”></menu>