Thursday, June 27, 2019

Run Powershell commands remotely with PSSession.


Here's a way to run Powershell commands on a remote machine.  It saves the hassle of RDP'ing into the remote machine, wait for Windows to load your profile, run startup scripts, apply GPO settings, load your desktop, etc...  That can be time consuming if you're always logging into to remote machine to verify something.

  1. Create a remote session
    • new-pssession <computer hostname>
  2. Get the ID of the remote session.
    • get-pssession
  3. Connect to that remote session using the ID.
    • enter-pssession <ID>
  4. Once connected, you should see the computer hostname in brackets like below.
    • [computername]: PS C:\users\guest
  5. Execute your Powershell command like so.
    • [computername]: PS C:\users\guest> get-volume
  6. To exit the session, just type the following.
    • exit or exit-pssession
  7. After exiting of the session, you would want to remove/close the session from your computer.
    • remove-pssession <id>
      • Leave me a comment if you know of a way to remove 'all' the sessions at once without looping it in a script.

Hope this helps.

No comments:

Post a Comment

Thank you for your comment.