Table of Contents

Method PSCopyContentTo

Namespace
IOInfoExtensions.PowerShell
Assembly
IOInfoExtensions.PowerShell.dll

PSCopyContentTo(PSObject, PSObject, bool, bool, bool)

PowerShell wrapper for the CopyContentTo method.
public static object PSCopyContentTo(PSObject source, PSObject destination, bool copyEmptyDirectories = false, bool overwrite = false, bool cleanTarget = false)

Remarks

This method copies all the files and directories within the calling DirectoryInfo object to the specified destination directory.

Parameters

source PSObject
The calling DirectoryInfo object to copy the contents of.
destination PSObject
The directory to copy all the contents to.
copyEmptyDirectories bool
If set to true empty child directories will be created in the destitnation.
overwrite bool
Overwrite any conflicting files at the destination.
cleanTarget bool
Delete all contents of the destination directory before copying.

Returns

object
N/A

Examples

Example 1
Copy files and their directory structures.
var source = new DirectoryInfo("C:\\Demo");
var destination = new DirectoryInfo("C:\\DemoCopy");

Console.WriteLine("Source Children:");
source.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine($"Destination Exists:\n\t{destination.Exists}");

Console.WriteLine("Copying content...");
source.CopyContentTo(destination);

Console.WriteLine($"Destination Exists after copy:\n\t{destination.Exists}");
Console.WriteLine("Destination Children:");
destination.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

/*
Output:
Source Children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
        C:\Demo\ChildDir2\ChildFile1.txt
        C:\Demo\ChildDir2\ChildFile2.txt
Destination Exists:
        False
Copying content...
Destination Exists after copy:
        True
Destination Children:
        C:\DemoCopy\ChildDir2
        C:\DemoCopy\ChildFile1.txt
        C:\DemoCopy\ChildFile2.txt
        C:\DemoCopy\ChildDir2\ChildFile1.txt
        C:\DemoCopy\ChildDir2\ChildFile2.txt
*/
$source = New-Object System.IO.DirectoryInfo 'C:\Demo'
$destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy'

Write-Host "Source Children:"
$source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "Destination Exists:`n`t$($destination.Exists)"

Write-Host "Copying content..."
$source.CopyContentTo($destination)

Write-Host "Destination Exists after copy:`n`t$($destination.Exists)"
Write-Host "Destination Children:"
$destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

<#
Output:
Source Children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
        C:\Demo\ChildDir2\ChildFile1.txt
        C:\Demo\ChildDir2\ChildFile2.txt
Destination Exists:
        False
Copying content...
Destination Exists after copy:
        True
Destination Children:
        C:\DemoCopy\ChildDir2
        C:\DemoCopy\ChildFile1.txt
        C:\DemoCopy\ChildFile2.txt
        C:\DemoCopy\ChildDir2\ChildFile1.txt
        C:\DemoCopy\ChildDir2\ChildFile2.txt
#>
Example 2
Copies all items, including empty directories.
var source = new DirectoryInfo("C:\\Demo");
var destination = new DirectoryInfo("C:\\DemoCopy");

Console.WriteLine("Source Children:");
source.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine($"Destination Exists:\n\t{destination.Exists}");

Console.WriteLine("Copying content...");
source.CopyContentTo(destination, true);

Console.WriteLine($"Destination Exists after copy:\n\t{destination.Exists}");
Console.WriteLine("Destination Children:");
destination.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

/*
Output:
Source Children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
        C:\Demo\ChildDir2\ChildFile1.txt
        C:\Demo\ChildDir2\ChildFile2.txt
Destination Exists:
        False
Copying content...
Destination Exists after copy:
        True
Destination Children:
        C:\DemoCopy\ChildDir1
        C:\DemoCopy\ChildDir2
        C:\DemoCopy\ChildFile1.txt
        C:\DemoCopy\ChildFile2.txt
        C:\DemoCopy\ChildDir2\ChildFile1.txt
        C:\DemoCopy\ChildDir2\ChildFile2.txt
*/
$source = New-Object System.IO.DirectoryInfo 'C:\Demo'
$destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy'

Write-Host "Source Children:"
$source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "Destination Exists:`n`t$($destination.Exists)"

Write-Host "Copying content..."
$source.CopyContentTo($destination, $true)

Write-Host "Destination Exists after copy:`n`t$($destination.Exists)"
Write-Host "Destination Children:"
$destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

<#
Output:
Source Children:
        C:\Demo\ChildDir1
        C:\Demo\ChildDir2
        C:\Demo\ChildFile1.txt
        C:\Demo\ChildFile2.txt
        C:\Demo\ChildDir2\ChildFile1.txt
        C:\Demo\ChildDir2\ChildFile2.txt
Destination Exists:
        False
Copying content...
Destination Exists after copy:
        True
Destination Children:
        C:\DemoCopy\ChildDir1
        C:\DemoCopy\ChildDir2
        C:\DemoCopy\ChildFile1.txt
        C:\DemoCopy\ChildFile2.txt
        C:\DemoCopy\ChildDir2\ChildFile1.txt
        C:\DemoCopy\ChildDir2\ChildFile2.txt
#>
Example 3
Copy files and their directory structures and overwrites any pre-existing files.
var source = new DirectoryInfo("C:\\Demo");
var destination = new DirectoryInfo("C:\\DemoCopy");

Console.WriteLine("Source Children:");
source.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("Destination Children:");
destination.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("File Information:");
var files = source.GetFiles().ToList().Concat(destination.GetFiles().ToList()).ToArray();
Console.WriteLine(GetFileTable(files, includeHash: true));

Console.WriteLine("Copying content...");
source.CopyContentTo(destination, false, true);

Console.WriteLine("Destination Children:");
destination.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("File Information after copy:");
files = source.GetFiles().ToList().Concat(destination.GetFiles().ToList()).ToArray();
Console.WriteLine(GetFileTable(files, includeHash: true));

/*
Output:
Source Children:
    C:\Demo\ChildDir1
    C:\Demo\ChildDir2
    C:\Demo\ChildFile1.txt
    C:\Demo\ChildFile2.txt
    C:\Demo\ChildDir2\ChildFile1.txt
    C:\Demo\ChildDir2\ChildFile2.txt
Destination Children:
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
File Information:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True AE7C049181E275A7AAF1E365339DBE888309C7BE
    C:\Demo\ChildFile2.txt       True 7A403B5B3FA0AA8386DC47AEEC9D03B3EBEB9A6C
    C:\DemoCopy\ChildFile1.txt   True BA699672FB60916D72552E903FBB6E7B0C342DD6
    C:\DemoCopy\ChildFile2.txt   True D66287A7B4BF2DB698341A97B06C8A8FF52661D7
    C:\DemoCopy\ChildFile3.txt   True FDE4898E14ECB550A6CCE74D06423F50DC2E83E1
Copying content...
Destination Children:
    C:\DemoCopy\ChildDir2
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
    C:\DemoCopy\ChildDir2\ChildFile1.txt
    C:\DemoCopy\ChildDir2\ChildFile2.txt
File Information after copy:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True AE7C049181E275A7AAF1E365339DBE888309C7BE
    C:\Demo\ChildFile2.txt       True 7A403B5B3FA0AA8386DC47AEEC9D03B3EBEB9A6C
    C:\DemoCopy\ChildFile1.txt   True AE7C049181E275A7AAF1E365339DBE888309C7BE
    C:\DemoCopy\ChildFile2.txt   True 7A403B5B3FA0AA8386DC47AEEC9D03B3EBEB9A6C
    C:\DemoCopy\ChildFile3.txt   True FDE4898E14ECB550A6CCE74D06423F50DC2E83E1
*/
$source = New-Object System.IO.DirectoryInfo 'C:\Demo'
$destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy'

Write-Host "Source Children:"
$source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "Destination Children:"
$destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "File Information:"
Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash

Write-Host "Copying content..."
$source.CopyContentTo($destination, $false, $true)

Write-Host "Destination Children:"
$destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "File Information after copy:"
Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash

<#
Source Children:
    C:\Demo\ChildDir1
    C:\Demo\ChildDir2
    C:\Demo\ChildFile1.txt
    C:\Demo\ChildFile2.txt
    C:\Demo\ChildDir2\ChildFile1.txt
    C:\Demo\ChildDir2\ChildFile2.txt
Destination Children:
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
File Information:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
    C:\Demo\ChildFile2.txt       True D7BB9BC327821EBD985C30F09F2F5B50B5418C79
    C:\DemoCopy\ChildFile1.txt   True 41AC012FA1E8DA02511EEFBA8F5A3B4F0BBE334A
    C:\DemoCopy\ChildFile2.txt   True ABB5D4DBD1812F92CF357CA7C0652ED66712232D
    C:\DemoCopy\ChildFile3.txt   True 24164ECDA682E2F8ABEB208672D868A1BA3BCC0E
Copying content...
Destination Children:
    C:\DemoCopy\ChildDir2
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
    C:\DemoCopy\ChildDir2\ChildFile1.txt
    C:\DemoCopy\ChildDir2\ChildFile2.txt
File Information after copy:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
    C:\Demo\ChildFile2.txt       True D7BB9BC327821EBD985C30F09F2F5B50B5418C79
    C:\DemoCopy\ChildFile1.txt   True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
    C:\DemoCopy\ChildFile2.txt   True D7BB9BC327821EBD985C30F09F2F5B50B5418C79
    C:\DemoCopy\ChildFile3.txt   True 24164ECDA682E2F8ABEB208672D868A1BA3BCC0E
#>
Example 4
Removes all contents of the destination and copies files and their directory structures.
var source = new DirectoryInfo("C:\\Demo");
var destination = new DirectoryInfo("C:\\DemoCopy");

Console.WriteLine("Source Children:");
source.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("Destination Children:");
destination.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("File Information:");
var files = source.GetFiles().ToList().Concat(destination.GetFiles().ToList()).ToArray();
Console.WriteLine(GetFileTable(files, includeHash: true));

Console.WriteLine("Copying content...");
source.CopyContentTo(destination, false, false, true);

Console.WriteLine("Destination Children:");
destination.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("File Information after copy:");
files = source.GetFiles().ToList().Concat(destination.GetFiles().ToList()).ToArray();
Console.WriteLine(GetFileTable(files, includeHash: true));

/*
Output:
Source Children:
    C:\Demo\ChildDir1
    C:\Demo\ChildDir2
    C:\Demo\ChildFile1.txt
    C:\Demo\ChildFile2.txt
    C:\Demo\ChildDir2\ChildFile1.txt
    C:\Demo\ChildDir2\ChildFile2.txt
Destination Children:
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
File Information:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True AE7C049181E275A7AAF1E365339DBE888309C7BE
    C:\Demo\ChildFile2.txt       True 7A403B5B3FA0AA8386DC47AEEC9D03B3EBEB9A6C
    C:\DemoCopy\ChildFile1.txt   True BA699672FB60916D72552E903FBB6E7B0C342DD6
    C:\DemoCopy\ChildFile2.txt   True D66287A7B4BF2DB698341A97B06C8A8FF52661D7
    C:\DemoCopy\ChildFile3.txt   True FDE4898E14ECB550A6CCE74D06423F50DC2E83E1
Copying content...
Destination Children:
    C:\DemoCopy\ChildDir2
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildDir2\ChildFile1.txt
    C:\DemoCopy\ChildDir2\ChildFile2.txt
File Information after copy:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True AE7C049181E275A7AAF1E365339DBE888309C7BE
    C:\Demo\ChildFile2.txt       True 7A403B5B3FA0AA8386DC47AEEC9D03B3EBEB9A6C
    C:\DemoCopy\ChildFile1.txt   True AE7C049181E275A7AAF1E365339DBE888309C7BE
    C:\DemoCopy\ChildFile2.txt   True 7A403B5B3FA0AA8386DC47AEEC9D03B3EBEB9A6C
*/
$source = New-Object System.IO.DirectoryInfo 'C:\Demo'
$destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy'

Write-Host "Source Children:"
$source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "Destination Children:"
$destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "File Information:"
Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash

Write-Host "Copying content..."
$source.CopyContentTo($destination, $false, $false, $true)

Write-Host "Destination Children:"
$destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "File Information after copy:"
Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash

Remove-DemoDirectory -RootPath 'C:\Demo'
Remove-DemoDirectory -RootPath 'C:\DemoCopy'

<#
Source Children:
    C:\Demo\ChildDir1
    C:\Demo\ChildDir2
    C:\Demo\ChildFile1.txt
    C:\Demo\ChildFile2.txt
    C:\Demo\ChildDir2\ChildFile1.txt
    C:\Demo\ChildDir2\ChildFile2.txt
Destination Children:
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
File Information:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
    C:\Demo\ChildFile2.txt       True D7BB9BC327821EBD985C30F09F2F5B50B5418C79
    C:\DemoCopy\ChildFile1.txt   True 41AC012FA1E8DA02511EEFBA8F5A3B4F0BBE334A
    C:\DemoCopy\ChildFile2.txt   True ABB5D4DBD1812F92CF357CA7C0652ED66712232D
    C:\DemoCopy\ChildFile3.txt   True 24164ECDA682E2F8ABEB208672D868A1BA3BCC0E
Copying content...
Destination Children:
    C:\DemoCopy\ChildDir2
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildDir2\ChildFile1.txt
    C:\DemoCopy\ChildDir2\ChildFile2.txt
File Information after copy:
    FullName                   Exists Hash
    --------                   ------ ----
    C:\Demo\ChildFile1.txt       True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
    C:\Demo\ChildFile2.txt       True D7BB9BC327821EBD985C30F09F2F5B50B5418C79
    C:\DemoCopy\ChildFile1.txt   True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
    C:\DemoCopy\ChildFile2.txt   True D7BB9BC327821EBD985C30F09F2F5B50B5418C79
#>
Example 5
Copy the contents to the target with conflicting items while not overwritting or cleaning the target.
var source = new DirectoryInfo("C:\\Demo");
var destination = new DirectoryInfo("C:\\DemoCopy");

Console.WriteLine("Source Children:");
source.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("Destination Children:");
destination.GetFileSystemInfos("*", SearchOption.AllDirectories)
    .ToList()
    .ForEach(x => Console.WriteLine($"\t{x.FullName}"));

Console.WriteLine("Copying content...");
try
{
    source.CopyContentTo(destination, false, false);
}
catch (Exception ex)
{
    var stringArgs = new[] {
        ex.GetType().ToString(),
        ex.Message,
        ex.StackTrace.Substring(0, ex.StackTrace.IndexOf(Environment.NewLine))
    };

    Console.Error.WriteLine(string.Format("\nError: {0}: {1}\n{2}\n", stringArgs));
}

/*
Output:
Source Children:
    C:\Demo\ChildDir1
    C:\Demo\ChildDir2
    C:\Demo\ChildFile1.txt
    C:\Demo\ChildFile2.txt
    C:\Demo\ChildDir2\ChildFile1.txt
    C:\Demo\ChildDir2\ChildFile2.txt
Destination Children:
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
Copying content...

Error: System.IO.IOException: The file 'C:\DemoCopy\ChildFile1.txt' already exists.
    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
*/
$source = New-Object System.IO.DirectoryInfo 'C:\Demo'
$destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy'

Write-Host "Source Children:"
$source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "Destination Children:"
$destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName |
    ForEach-Object { Write-Host "`t$_" }

Write-Host "Copying content..."

try
{
    $source.CopyContentTo($destination, $false, $false)
}
catch
{
    $stringArgs = @(
        $_.Exception.GetType().ToString(),
        $_.Exception.Message,
        $_.Exception.StackTrace.Substring(0, $_.Exception.StackTrace.IndexOf([Environment]::NewLine))
    )

    Write-Host ("`nError: {0}: {1}`n{2}`n" -f $stringArgs)
    $error.Clear()
}

<#
Source Children:
    C:\Demo\ChildDir1
    C:\Demo\ChildDir2
    C:\Demo\ChildFile1.txt
    C:\Demo\ChildFile2.txt
    C:\Demo\ChildDir2\ChildFile1.txt
    C:\Demo\ChildDir2\ChildFile2.txt
Destination Children:
    C:\DemoCopy\ChildFile1.txt
    C:\DemoCopy\ChildFile2.txt
    C:\DemoCopy\ChildFile3.txt
Copying content...

Error: System.IO.IOException: The file 'C:\DemoCopy\ChildFile1.txt' already exists.
    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
#>

Exceptions

PSInvalidOperationException