Table of Contents

Method PSCopyFrom

Namespace
IOInfoExtensions.PowerShell
Assembly
IOInfoExtensions.PowerShell.dll

PSCopyFrom(PSObject, PSObject, bool)

PowerShell wrapper for the FileInfo.CopyFrom method.
public static object PSCopyFrom(PSObject destination, PSObject source, bool overwrite = false)

Remarks

Copies the source file to the calling destination. If the destination exists and overwrite is false, an exception will be thrown.

System.IO.FileInfo has a CopyTo method, but it changes the source FileInfo object information to match the destination FileInfo properties. CopyFrom will allow you to call the copy from the destination, leaving the source object still referencing the source file.

Parameters

destination PSObject
The calling FileInfo object.
source PSObject
The source FileInfo object to be copied.
overwrite bool
Indicates if the destination file should be overwritten if it exists.

Returns

object
N/A

Examples

Example 1
Copies and renames a file.
var sourceFile = new FileInfo("C:\\Demo\\ChildFile1.txt");
var destinationFile = new FileInfo("C:\\DemoCopy\\ChildDir3\\ChildFile3.txt");

Console.WriteLine("File Information:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true));

Console.WriteLine("Copying file...");
destinationFile.CopyFrom(sourceFile);

Console.WriteLine("File Information after copy:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true));

/*
File Information:
        Directory             Name           DirectoryExists Exists
        ---------             ----           --------------- ------
        C:\Demo               ChildFile1.txt            True   True
        C:\DemoCopy\ChildDir3 ChildFile3.txt           False  False
Copying file...
File Information after copy:
        Directory             Name           DirectoryExists Exists
        ---------             ----           --------------- ------
        C:\Demo               ChildFile1.txt            True   True
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True
*/
$sourceFile = New-Object System.IO.FileInfo 'C:\Demo\ChildFile1.txt'
$destinationFile = New-Object System.IO.FileInfo 'C:\DemoCopy\ChildDir3\ChildFile3.txt'

Write-Host "File Information:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory)

Write-Host "Copying file..."
$destinationFile.CopyFrom($sourceFile)

Write-Host "File Information after copy:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory)

<#
Output:
File Information:
        Directory             Name           DirectoryExists Exists
        ---------             ----           --------------- ------
        C:\Demo               ChildFile1.txt            True   True
        C:\DemoCopy\ChildDir3 ChildFile3.txt           False  False
Copying file...
File Information after copy:
        Directory             Name           DirectoryExists Exists
        ---------             ----           --------------- ------
        C:\Demo               ChildFile1.txt            True   True
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True
#>
Example 2
Copies and overwrites a file.
var sourceFile = new FileInfo("C:\\Demo\\ChildFile1.txt");
var destinationFile = new FileInfo("C:\\DemoCopy\\ChildDir3\\ChildFile3.txt");

Console.WriteLine("File Information:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));

Console.WriteLine("Copying file...");
destinationFile.CopyFrom(sourceFile, true);

Console.WriteLine("File Information after copy:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));
/*

Output:
File Information:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True AE7C049181E275A7AAF1E365339DBE888309C7BE
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True 6633A8AA22899FBB94984085B76796D33A890836
Copying file...
File Information after copy:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True AE7C049181E275A7AAF1E365339DBE888309C7BE
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True AE7C049181E275A7AAF1E365339DBE888309C7BE
*/
$sourceFile = New-Object System.IO.FileInfo 'C:\Demo\ChildFile1.txt'
$destinationFile = New-Object System.IO.FileInfo 'C:\DemoCopy\ChildDir3\ChildFile3.txt'

Write-Host "File Information:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)

Write-Host "Copying file..."
$destinationFile.CopyFrom($sourceFile, $true)

Write-Host "File Information after copy:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)

<#
Output:
File Information:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True 456012073991C95AF2F3628D2C28595A85E80653
Copying file...
File Information after copy:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
#>
Example 3
Throws an error because the destination already exists.
var sourceFile = new FileInfo("C:\\Demo\\ChildFile1.txt");
var destinationFile = new FileInfo("C:\\DemoCopy\\ChildDir3\\ChildFile3.txt");

Console.WriteLine("File Information:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));

Console.WriteLine("Copying file...");
try
{
    destinationFile.CopyFrom(sourceFile);
}
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));
}

Console.WriteLine("File Information after copy:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));

/*
Output:
File Information:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True AE7C049181E275A7AAF1E365339DBE888309C7BE
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True 6633A8AA22899FBB94984085B76796D33A890836
Coyping file...

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

File Information after copy:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True AE7C049181E275A7AAF1E365339DBE888309C7BE
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True 6633A8AA22899FBB94984085B76796D33A890836
*/
$sourceFile = New-Object System.IO.FileInfo 'C:\Demo\ChildFile1.txt'
$destinationFile = New-Object System.IO.FileInfo 'C:\DemoCopy\ChildDir3\ChildFile3.txt'

Write-Host "File Information:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)

Write-Host "Copying file..."

try
{
    $destinationFile.CopyFrom($sourceFile)
}
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()
}

Write-Host "File Information after copy:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)

<#
Output:
File Information:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True 456012073991C95AF2F3628D2C28595A85E80653
Copying file...

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

File Information after copy:
        Directory             Name           DirectoryExists Exists Hash
        ---------             ----           --------------- ------ ----
        C:\Demo               ChildFile1.txt            True   True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
        C:\DemoCopy\ChildDir3 ChildFile3.txt            True   True 456012073991C95AF2F3628D2C28595A85E80653
#>

Exceptions

FileNotFoundException
If the source file does not exist.
IOException
If the destination file exists but overwrite is not set to true.
PSInvalidOperationException