How to Copy Files To-From a Remote Machine using VSC Answer
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
How can I Copy files To-From a Remote Machine Using VSC Answer
- You should have completed the prerequisites from the Copy to-from Challenge.
- Let’s review – On the Host you have a file called loginfo.txt located in C:\Temp\Local folder.
On the Remote computer you have a file called Kb.pdf located in C:\Temp\Remote folder
- In this lecture we will use VSC and the Copy-item command to copy files to and from a remote computer.
- This lecture is not meant to be part of a VSC course. I just thought you might like to learn a few things about Visual Studio Code.
- And all scripts that we will take a look at will work in PowerShell ISE as well as VSC.
If you have already followed the directions from Section 2 called “Setting up Visual Studio Code for Powershell” you will be ready to use these commands.
From the Host computer open VSC.
You will need to create a remote Session to the target computer VSC1 using the New-PSSession cmdlet.
Here is the command line:
I already have my script in the folder section. You can go ahead and Copy and Paste this command into the Terminal section of VSC
$session = New-PSSession -ComputerName VSC1 -Credential (Get-Credential)
Here is the Explanation:
$session: This is a variable that will store the session object created by New-PSSession. This object will be used to interact with the remote computer.
New-PSSession: This cmdlet is used to create a new PowerShell session that allows you to run commands on a remote computer.
-ComputerName VSC1: Here, you specify the target remote computer's name or hostname, which in this case is "VSC1." This is the computer you want to connect to remotely.
-Credential (Get-Credential): This part of the command is responsible for specifying the credentials to use for the remote session.
Get-Credential: This is a cmdlet used to interactively prompt the user for a username and password. It opens a secure dialog box for you to enter the necessary credentials. The credentials you enter here are the credentials that will be used to authenticate and establish the remote connection to "VSC1."
When you use PowerShell Remoting to copy a file from your local computer to a remote computer, you are running the commands from your local computer, but those commands are executed on the remote computer. You establish a remote session to the target computer, but you remain on your local computer while executing commands.
I’ll go ahead and press F8 and run the command
I’ll type administrator, then the password. Now my remote session has started.
- Let’s go ahead and learn how to use the Copy-item command to copy a file from the local computer, to the C:\Temp\remote folder on the remote VM.
Here is the command line and the explanation:
Copy-item -Path C:\Temp\Local\loginfo.txt -Destination c:\Temp\Remote -ToSession $Session
- Copy-item - This is the cmdlet used to copy files and directories in PowerShell.
- -Path - This parameter specifies the source file or directory you want to copy. In this case, you are copying the file loginfo.txt located at C:\Temp\Local\ on your local computer.
- -Destination: This parameter specifies the destination directory where you want to copy the source file or directory. You want to copy it to C:\Temp\Remote on the remote computer.
- -ToSession: This parameter specifies the PowerShell session (PSSession) that you want to use for the copy operation. You've stored a session object in the $Session variable, which represents your connection to the remote computer.
- Let’s go ahead and run the Copy-Item command
- Verify that your file was copied to the C:\Temp\Remote folder on the remote computer.
There we see loginfo.txt, so that was successful. - Now take a look at the command line that copies files from the remote computer to the local computer.
You should have already copied KB.pdf to the C:\Temp\Remote folder - Here is the command line:
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.