Copy file to and from Powershell remote session

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're gonna look at copying files to and from using PowerShell now here we have a file that we're gonna use for our test purposes and we're gonna open up a new PS session now for those of you coming from a Linux world you'll be thinking something like SCP similar principle so here we're going to open a new PS session we're going to put it into the variable and there we now have a connection to our server one and if I do a get PS session I can see that the session is actually open now we can go ahead and explore that a little bit more bite as an example we can now run an invoke command we can tell it hey use the session this is the name of my session this is the script block I want you to run and I'm gonna give you a command now there's gonna be no response from this because LS in this particular example is gonna run against the directory of the user and that directory is empty so there's no output but that could easily be a different output that could be something more usable or tangible just giving you an example that you can use invoke command so let's do a different one let's go ahead and connect to our machine so we do an enter PS session name of the session so again we're using our session here so we're not creating a new one we're just logging into the existing one and as previously stated the LS returns nothing because it's an empty directory so I'm not gonna go down to the root of C and just show you the output now the reason I'm doing this is because I've got a test file sitting on the C disk here and I've got the test file sitting in my test folder on my current machine so what I want to do is copy the files back and forth so I'm going to take the file from the remote machine and copy it to local and vice versa I'm going to take the local machine and copy the file to the remote one now important thing here if I open up a new powershell session and just do a PS session you'll see that the session doesn't exist so everything I'm doing is within this terminal if I change terminal it's different so I'm gonna go back out so I'm no longer directly in that session but I still have my PS terminal open I'm gonna run the copy item command and then I'm going to tell it the path of the file so in this case this is the file that you want to move so in this case is now the one locally on my machine then I have to set the destination so this is going to be the location on the remote machine and finally I need to tell it which direction this is going so this is the part where we have a - from or - so in this case we're gonna use the - session and then the name of the session so I'm sending the file from the local to the remote now same principle in Reverse if I use from and then we're now sending the file from the remote machine to my local machine so here I no need to put the path of the local and I need to change the other one to the path of the source which in this case is the remote and we run that again so what I've now done is copied the remote file to the local file now the remote file was empty so there's nothing in here so sadly that's a bad example but my local file does have text so I can now go into the remote session again so I just do it again another inter PS session and what I'm gonna do is go check the content of the file that I've just copied there so if I do another LS you can see we have the new file that I created so from remote and I can do a get content to prove that the content is actually there this does take a little while type out I probably should have sped this up sorry for that but as you can see the content of the file exists so the copying from one location theater has in fact been successful now since we did open up this session and we've been in and out of it probably we should look at also looking at how to close sessions so you can get it PS session and you can see where the session is and the command for closing them remove PS session now you could give an exact session ID though you can use the piping like I have to close all of the open sessions so you don't need to think about it
Info
Channel: Tips For IT Pros
Views: 16,557
Rating: undefined out of 5
Keywords: powershell copy-item, powershell remote session, remote powershell session, Tip For IT Pros, Powershell, powershell remoting tutorial, Powershell for beginners
Id: P1zDkxSpzaQ
Channel Id: undefined
Length: 4min 36sec (276 seconds)
Published: Tue May 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.