I need Robocopy to copy all folders starting with example 6000 to new server. It should also create the folders at the new server in same script. I try something like this:
$Source=(Get-Item \server01\OldFiles | Where-Object {$.PSIsContainer -and $.Name -like “6000*”}).Name
Foreach ($Source in $Source){
robocopy \server01\OldFiles$Source \Server02\6000- /MIR /xo /COPY:DATS
}
But It takes only what is inside folders starting with 6000 to new server. I need script to create folders also and then copy all insite the folders. Can anyone help?

5 Spice ups

Can try something like this?
I don’t use robocopy, so this is untested.

Based on what your query is though, should make work?

$SourceRoot = "\\server01\OldFiles"
$DestRoot = "\\Server02\OldFiles"

$Folders = Get-ChildItem -Path $SourceRoot -Directory -Recurse | Where-Object { $_.Name -like "6000*" }

foreach ($Folder in $Folders) {
    $SourcePath = Join-Path $SourceRoot $Folder.Name
    $DestPath = Join-Path $DestRoot $Folder.Name

    # Create destination folder if it doesn't exist
    if (-not (Test-Path -Path $DestPath)) {
        New-Item -Path $DestPath -ItemType Directory | Out-Null
    }


   Start-Process "robocopy" -ArgumentList {$SourcePath, $DestPath, "/MIR" "/XO" "/COPY:DATS"}
}
2 Spice ups

I get this Get-ChildItem : Could not find a part of the path
At line:4 char:12

  • $Folders = Get-ChildItem -Path $SourceRoot -filter “6000*” -Directory
    But if I use this the folders is created but nothing content in all the created folders.
    $folders = Get-ChildItem \server01\OldFiles -filter “6000” -Directory

foreach ($Folder in $Folders) {
$SourcePath = Join-Path $SourceRoot $Folder.Name
$DestPath = Join-Path $DestRoot $Folder.Name

# Create destination folder if it doesn't exist
if (-not (Test-Path -Path $DestPath)) {
    New-Item -Path $DestPath -ItemType Directory | Out-Null
}

Start-Process “robocopy” -ArgumentList {$SourcePath, $DestPath /“MIR” /“XO” /“copy:DATS” /“MT:32”}
}

2 Spice ups

I think your main issue is the $source variable. Get-Item only returns specifically what is defined in the supplied Path. If you want subfolders of the path, then you either need Get-ChildItem or use a wildcard in the path.

You also want a different current item variable in your foreach than your collection variable instead of $source for both.

$SourceDirs = (Get-Item \\server01\OldFiles\6000* | Where PSIsContainer).Name
# loop through directories
foreach ($Source in $SourceDirs) {
    # your robocopy code
}
2 Spice ups

I added -Recurse to crawl through the folders into the $SourceRoot which should possibly counter this.

2 Spice ups

Now I make this script. And it create folders and copy everything correct. BUT I also need permission. It seem that /copy:DATS not is working. anyone help with this?
$SourceRoot = “\server01\OldFiles”
$DestRoot = “\Server02\OldFiles”

Find folders starting with Example “600*”

$Folders = Get-ChildItem \server01\OldFiles -filter “6000*” -Directory

foreach ($Folder in $Folders) {
$SourcePath = Join-Path $SourceRoot $Folder.Name
$DestPath = Join-Path $DestRoot $Folder.Name

# Create destination folder if it doesn't exist
if (-not (Test-Path -Path $DestPath)) {
    New-Item -Path $DestPath -ItemType Directory | Out-Null
} 

Start-Process “robocopy” -ArgumentList {$SourcePath, $DestPath /“MIR” /“XO” /“copy:DATS” /“E”}

robocopy \\server01\OldFiles\$folder \\Server02\OldFiles\$folder /copy:DATS /E /MT:32

}

1 Spice up

You should use backticks or the code snippets (CTRL + e) to help ys determine the proper code parts.

You’re using both start-process and the robocopy choose one or the other.
Also… the slashes need to be withing the quotes.

Start-Process "robocopy.exe" -ArgumentList {$SourcePath, $DestPath, "/MIR", "/XO", "/copy:DATS", "/E", "/MT:32"}
1 Spice up

$sourceRoot = “\server01\OldFiles”
$destinationRoot = “\server02\OldFiles”

Get all folders starting with 6000

$folders = Get-ChildItem -Path $sourceRoot -Directory | Where-Object { $_.Name -like “6000*” }

foreach ($folder in $folders) {
$sourcePath = Join-Path $sourceRoot $folder.Name
$destinationPath = Join-Path $destinationRoot $folder.Name

# Ensure destination folder exists
if (-not (Test-Path $destinationPath)) {
    New-Item -ItemType Directory -Path $destinationPath | Out-Null
}

# Use Robocopy to mirror contents
robocopy $sourcePath $destinationPath /MIR /XO /COPY:DATS /R:2 /W:5

}

1 Spice up

Not sure how this differs from the solution I provided?
Then again, it’s hard to read without the code / preformatted text.

1 Spice up

I ended to do this who is creating folders at the new server and copying all content also. It alse keep permission at all folders. So if others need the same this is the solution:
$Folders = Get-ChildItem \server01\OldFiles\ -filter “600*” -Directory
foreach ($Folder in $Folders) {
robocopy \server01\OldFiles$folder \server02\OldFiles$folder /copy:DATS /E /MT:32
}