Learn How to Manage Remote PowerShell Sessions

Summary: Microsoft Scripting Guy Ed Wilson discusses how to manage remote Windows PowerShell sessions.


Hey, Scripting Guy! QuestionHey, Scripting Guy! I can see where being able to run a command on a remote computer might be kind of cool. But more often than not, I need to actually sit at the console of the remote computer. For this, I have been using remote desktop protocol (RDP) to display the remote desktop. I then open Windows PowerShell and do my work. It is okay, but a bit slow. I am wondering if there is a better way to do things?



Hey, Scripting Guy! AnswerHello CH,

Microsoft Scripting Guy Ed Wilson here. Well things are certainly getting exciting. It is now official—there is a Windows PowerShell Users group in Pittsburgh, Pennsylvania, in the United States. The first meeting is on December 13, 2011, from 6:00 P.M. until 8:00 P.M., and the Scripting Wife and I have been invited to the first meeting. Mark your calendars! If you are within driving range (or even a short flight), you do not want to miss this exciting presentation. The meeting will be held at the Microsoft office in downtown Pittsburgh. I will be speaking about Windows PowerShell best practices (and the Scripting Wife will be there signing autographs, and being nice and fun to talk to). Make sure you go to the PowerShell Community Groups and join the Pittsburgh PowerShell Users Group, and register for the meeting. This will allow the president of the group, Ken McFerron, to know how much food to order for the meeting.

CH, the first thing I do when working with remoting is use the Get-Credential cmdlet to store a credential object in a variable. This allows for a great deal of flexibility, and permits me to try various commands without the need to constantly type the user name and password. The command here prompts for the password for the administrator account of the Contoso domain:

$cred = Get-Credential -Credential contoso\administrator

If all I need to do is to run a few Windows PowerShell commands on the remote computer, I will go ahead and directly enter into a Windows PowerShell session. To do this, I use the Enter-PSSession cmdlet and specify both the remote computer name as well as the credentials to use. Here is the command I use to enter into a remote Windows PowerShell session:

Enter-PSSession -ComputerName syddc01 -Credential $cred

In the following figure, I store credentials in a variable, enter a remote Windows PowerShell session, and use the hostname.exe command to determine the name of the computer to which I am connected.

Image of determining name of computer to which connected

After I enter a remote Windows PowerShell session, I can use the Windows PowerShell cmdlets without worrying about firewall issues, remote credentials, or anything. The cmdlets work as if I were sitting at the Windows PowerShell console. For example, I can use Get-Process or Get-Service directly without specifying computername parameters. This is great because neither Get-Process nor Get-Service exposes a credential parameter, so it is more complicated to run the commands with alternate credentials.

In the following figure, I am connected to the remote server, SydDc01. I first examine the explorer process. Next, I look for services that contain the letters WMI. Next, I look for services that contain the letters ws. I then exit the remote session, and run the same commands on the local host. The great thing is the remote session, and the local session share the same Windows PowerShell command history. Therefore, I can simply up-arrow to retrieve the previous commands.

Image of being connected to remote server, SydDc01

I can even enter a remote Windows PowerShell session on another computer, and still use the same history of commands. This makes it really convenient to run the same commands on multiple computers.

To see if there are any Windows PowerShell sessions running, I use the Get-PSSession cmdlet. This is the syntax:


One of the things I like to do is to store a session. This allows me to enter and leave the remote Windows PowerShell session without worrying about overhead. To do this I use the Get-Credential cmdlet to obtain a credential object to use with the remote computer for creating a remote Windows PowerShell session. I then store the credential object in a variable. Syntax for this command is shown here:

$cred = Get-Credential

Next, I use the New-PSSession cmdlet to create a new session to a remote machine. I specify a name for the session, and the computername of the remote computer. For credentials, I use the credentials I stored in the $cred variable. I store the returned PSSession object in a variable:

$syddc01 = New-PSSession -Name syddc01 -ComputerName syddc01 -Credential $cred

Now that I have a PSSession stored in a variable, I can use the Enter-PSSession cmdlet to enter into a remote Windows PowerShell session. Here is the command I use to enter the PSSession stored in the $syddc01 variable:

Enter-PSSession $syddc01

I can now work in the Windows PowerShell remote console as if I were working on my local computer. I can use the Get-Service cmdlet to return information about the bits service. I can also use wildcard characters with Get-Service to display information about every service that matches a wildcard pattern. I can then exit the Windows PowerShell session by using the exit command. Next, I can use the same commands on my local computer, and then return to the remote session by again using the Enter-PSSession cmdlet. I can again exit the session, and run commands locally. These commands are shown here:

Get-Service -name bits 



Get-Service -Name *ii*

Enter-PSSession $syddc01

Get-Service *net*


Get-Service *net*

The commands and associated output are shown in the following figure.

Image of commands and associated output

After I am finished with my remote work, I remove the PSSession; removing unused PSSessions frees up resources. The easy way to remove a PSSession is to pipe the results of Get-Session to Remove-PSSession. This command is shown here:

Get-PSSession | Remove-PSSession

CH, that is all there is to reusing a Windows PowerShell remote session. Join me tomorrow as I talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.


Ed Wilson, Microsoft Scripting Guy


Comments (23)

  1. Boe Prox says:

    This is known as a ‘double hop’ and cannot be done unless you enable CredSSP delegation on that server you using for the possession. You can either look at using Delegated Administration or use the Enable-CredSSP cmdlet to set this up. Some links below for assistance.



  2. Anonymous says:

    Awesome post Ed!

  3. I love remoting! To make things easier for me, I store an encrypted password as a file and read it in when as part of my profile.

    $Password = Get-Content C:Usersjspattoncred.txt |ConvertTo-SecureString

    $Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "DomainAdministrator", $Password

  4. Klaus Schulte says:

    Hi Ed,

    PS remoting really rocks!

    It is something that you have to become used to.

    It's easy and very efficient!

    But, if you haven't been working with remote machines on a regular basis, it might be  a little bit confusing … and you may not always be aware of "WHERE you are" 🙂

    So: Be careful!


  5. Craig says:

    You may need to RDP onto the remote machine and run PowerShell as Administrator and then run "winrm quickconfig" before the above commands will work.

  6. hardheadedallen says:

    So, if I am using a computer that is part of Domain A, let's say…and I want to run active directory cmdlets against a trusted domain called Domain B, let's say…..how do I specify the domain in a cmdlet so I can administer Domain B?

  7. Juan Garcia says:

    This is an amazing post… Thanks a bunch…

  8. David Egan says:

    This is a wonderful post so useful.

  9. CP says:

    Great guide, its helped a lot thanks.

    I wonder if you can help though. Once connected to the remote session I am trying to run a batch job from a unc path and even though I am an administrator, i am getting access denied.

    $cred = get-cred # I supply a domain admin account
    $x=new-pssession -computername x -credential $cred
    enter-pssession $x -credential $cred
    [x]: PS c:> set-location \somew2k8serverc$

    Set-Location : Access is denied
    + CategoryInfo : PermissionDenied: (\somew2k8serverc$:String) [Set-Location], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.SetLocationCommand

    Set-Location : Cannot find path ‘\somew2k8serverc$’ because it does not exist.
    + CategoryInfo : ObjectNotFound: (\somew2k8serverc$:String) [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

  10. CP says:


    Much appreciated.

  11. gjjh says:

    Some individuals use steel glass ms windows, but these are just as unpleasant, and may skip out like wood.


  12. the venus factor diet reviews says:

    The best part is that you get a full 60 day refund policy if you are not satisfied so you really have nothing to reduce.

  13. the venus factor diet reviews says:

    Women all over the world are accomplishing amazing outcomes which means the Venus Aspect works. The simple to understand strategy that it has makes fat decrease fun and you will get motivation with the fast outcomes you see.

  14. ak34 says:

    I’d like to know why this isn’t working when run from .ps1 script. After entering the session, I try to run Get-Process, but instead of returning the list of process from the remote computer, it lists all the process running on my local computer. Should
    it be this simple?

    Enter-PSSession $test
    Get-Process | select ProcessName

    Thank you in advance, PS beginner here.

  15. ak34 says:

    Sorry, I already found out that I should use the Invoke-Command. Is there any other way? For example to just pipe the commands after entering the remote session?

    Thank you

  16. Quick Property Sale says:

    Up to 5% of the asking cost can be saturated up this way and somehow the agent always seems to gather on this bonus!

  17. visit this site right here says:

    Government authorities can not let the lengthy run of the toy protection seriously.

  18. bangla album says:

    THanks for such great article

  19. YohBur says:

    I need to import modules on the remote computer to be able to makea set-location ad: on the remote computer.
    When I run my commands remotly with a New-PSSession, I have no error, but When I try to run the Get-Acl command on the AD directories (that needs to be in the drive AD:), it doesn’t work.
    Have you some idea about this?

    Thanks in advance for any help

  20. YohBur says:

    I found the solution:
    With my "invok-command", my parameter was not transmited. So with this new command, it’s work well:
    $Out = invoke-command -Session $s {param($param) Get-Acl $param) -arg $Path

  21. YC says:

    Anybody knows what does this exception means? And how to properly handle this exception?

    Remove-PSSession : Safe handle has been closed
    + Get-PSSession | Remove-PSSession <<<<
    + CategoryInfo : NotSpecified: (:) [Remove-PSSession], ObjectDisposedException
    + FullyQualifiedErrorId : System.ObjectDisposedException,Microsoft.PowerShell.Commands.RemovePSSessionCommand

  22. mike says:

    I’m am use to a simple:
    ssh user@host

    to log in. None of this works on a the corporate setup I’m on were they are trying to stop this kind of stuff. What ports and/or services need to be functional on both the client and the host server to allow remote shells on Windows.

  23. Frank says:

    This was helpful. But on a corporate network, getting a PS is ridiculous and these methods didn’t work either. Too much burden is placed on the end user. Is the connection to a workgroup or domain or a Windows sever instead of a desktop all require you
    do do something different and know these things.

    Contrast that to the out of the box installations for most Linux, *BSD and OSX which host OpenSSH (or compatible version) which the end user doesn’t need to know anything except a user account to make a connection as long as there is a working network all with
    in an encrypted session. But one software vendor does not want to play with the other kids in the sandbox and provide a lowest common denominator for network connectivity in a heterogeneous computer environment. Go figure.

Skip to main content