Project maintained by timhaintz Hosted on GitHub Pages — Theme by mattgraham

Copying files to other machines: New-PSSession & Copy-Item

Introduction

On occassion, files need to be copied from one machine to another. This can include times where the machines are in different domains, or elevated permissions are required to fulfil the task. There are quite a few methods to do this.

Below, I use Get-Credential, New-PSSession and Copy-Item. From the Notes section of the New-PSSession documentation This cmdlet uses the Windows PowerShell remoting infrastructure. To use this cmdlet, the local computer and any remote computers must be configured for Windows PowerShell remoting. Please ensure PowerShell Remoting is setup.

The folder and file layout on my local machine is as below:

PS C:\Users\azureadmin\test> $env:COMPUTERNAME
ca1

PS C:\Users\azureadmin\test> ls -Recurse

    Directory: C:\Users\azureadmin\test

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         5/8/2018   8:32 PM                Info
    Directory: C:\Users\azureadmin\test\Info

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         5/8/2018   8:32 PM                Folder1
d-----         5/8/2018   8:32 PM                Folder2

    Directory: C:\Users\azureadmin\test\Info\Folder1

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/8/2018   8:32 PM             14 Test1.txt
-a----         5/8/2018   8:32 PM              0 Test2.txt

    Directory: C:\Users\azureadmin\test\Info\Folder2

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/8/2018   8:32 PM              0 Test3.txt
-a----         5/8/2018   8:32 PM              0 Test4.txt

To copy the folders and files, the below PowerShell commands are run.

PowerShell Code Block

$credential = Get-Credential timhaintz\azureadmin
$session = New-PSSession -ComputerName dc1 -Credential $credential
Copy-Item -Recurse "C:\Users\azureadmin\test" -Destination "C:\test\" -ToSession $Session

The first line $credential = Get-Credential…… prompts for a username and password. In a future blog, I will use Export-Clixml to export the credentials into an XML file, so as to remove the need for user interaction.

The New-PSSession example is taken straight from Microsoft’s documenation, as is Copy-Item.

To interactively connect to the destination server Enter-PSSession can be utilised.

PS C:\Users\azureadmin\test> Enter-PSSession -Session $session

[dc1]: PS C:\test>

From this session, you can check if the files and folders copied correctly.

[dc1]: PS C:\test> $env:COMPUTERNAME
dc1

[dc1]: PS C:\test> ls -Recurse


    Directory: C:\test


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         5/8/2018   8:36 PM                Info


    Directory: C:\test\Info


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         5/8/2018   8:36 PM                Folder1
d-----         5/8/2018   8:36 PM                Folder2


    Directory: C:\test\Info\Folder1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/8/2018   8:32 PM             14 Test1.txt
-a----         5/8/2018   8:32 PM              0 Test2.txt


    Directory: C:\test\Info\Folder2


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         5/8/2018   8:32 PM              0 Test3.txt
-a----         5/8/2018   8:32 PM              0 Test4.txt

As displayed above, the folders and files from the local machine are successfully copied to the destination machine using a session.

Hope this is of use.

Thanks, Tim.