Powershell: Copy file to local machine *after* Enter-PSSession

Flint Stone 1 Reputation point
2020-11-26T09:00:06.48+00:00

I know that I can use Copy-Item with -ToSession or -FromSession to transfer a file using WinRM. But my Use-Case is, that I have already used Enter-PSSession REMOTEPC to work on another PC and now (inside of this session) I want to copy a file from the REMOTEPC back to my local PC using WinRM. Is this possible with -ToSession ? And what would I have to specify as argument?

Windows for business | Windows Server | User experience | PowerShell
{count} votes

4 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2020-11-30T19:16:41.447+00:00

    Why not do something like this?

    $s1 = New-PSSession -ComputerName xxx -Credential (Get-Credential)
    $s2 = New-PSSession -ComputerName zzz -Credential (Get-Credential)
    Enter-PSSession $s1
    .
    . # do stuff on xxx
    .
    Exit-PSSession
    Enter-PSSession $s2
    .
    . # do stuff on zzz
    .
    Exit-PSSession
    # now you're back on local machine
    Copy-Item C:\Temp\xxx.txt -destination c:\junk -FromSession $s1 -ToSession $s2  # between 2 remote sessions
    Copy-Item C:\AnotherDirectory\SomeotherFile.txt -Destination -FromSession $s2   # from remote to local
    Enter-PSSession $s1
    . . . Continue working
    Exit-PSSession
    $s1,$s2 | Remove-PSSession
    
    1 person found this answer helpful.

  2. Andreas Baumgarten 123.6K Reputation points MVP Volunteer Moderator
    2020-11-26T10:04:05.477+00:00

    Maybe this is helpful:
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.1#example-9--copy-a-remote-file-to-the-local-computer

    I am not sure if this is working with Enter-PSSession or if it has to be New-PSSession like in the example. But it's worth a try I think.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  3. Anonymous
    2020-11-27T04:16:13.757+00:00

    Hi,

    You can create a new session back to your local machine then copy the file to it

    $session = New-PSSession -ComputerName $localmachine -Credential $cred  
    Copy-Item -Path $file -Destination $localpath -ToSession $session  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. Brian Yerk 1 Reputation point
    2021-04-23T15:06:17.507+00:00

    Old post but maybe this will help someone going forward- Found this which will help me in a high sec environment like the one I work in- https://blog.ipswitch.com/use-powershell-copy-item-cmdlet-transfer-files-winrm

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.