PowerShell Not your Father’s Command Line Part 21 of 31: Knock Knock PowerShell Calling!


Knock, Knock
Who's there?
Sarah who?
Sa-rah phone I could use?

In PowerShell 1.0 remoting was much like my joke, there was no direct way to have a remote PowerShell session.  You may have used remote desktop services or other methods to access the PowerShell on remote systems.  With PowerShell 2.0 remoting is now built-in and provides an invaluable addition to PowerShell functionality.  Regardless of the OS, whether it is Windows 7, Windows Server 2008 R2 or any other systems that support PowerShell 2.0 you can remote to the systems.  Under the covers remoting uses the WinRM service to setup a secure connection for your remote sessions. 

One of the great things about remoting is how quickly you can enable it.  You will have to enable remoting on both the systems you want to remote from and remote to, and you will also need to run the following cmdlet from an administrative PowerShell session:


This will enable all remoting, configure the firewall, and even setup the WinRM service for you to use…etc.  However, the counterpart to this cmdlet Disable-PSRemoting, will start the process to disable remoting, after you run that cmdlet you will show you the additional steps you will need to perform to finish removing remoting.   After you have enabled remoting you have a couple of ways you can work with remote systems in PowerShell. One way is you can use the Invoke-Command cmdlet to run a script block on the server or servers you want to administer with PowerShell.  The next example, from the PowerShell examples, shows of a niffty cmdlet Get-Culture,(which shows you the language settings for a particular server).  This example would run Get-Culture and return results for Server1, Server2, and Server3:

Invoke-Command –ComputerName Server1, Server2, Server3 -scriptblock{Get-Culture}

Invoke-Command is great for a quick hit into manage your remote servers.  With PowerShell remoting you can also setup a persistent connection using the New-PSSession cmdlet.  You can even make your remote sessions really stick around by placing the remote sessions you want in your PowerShell profiles (which we covered in Part 9).  The following will create a remote session for Server2:

New-PSSession -ComputerName Server2

While the session is created it is just hanging out in the background waiting for you to access it.  To enter the session for  Server2 you would run the following command:

Enter-PSSession Server2

You will notice after you have entered the session your PowerShell prompt will look similar to this: Server2:\PS>  Once you enter the session you are in the an interactive PowerShell session  with the server, and you can run any cmdlets that are on the system has loaded on it.  After you are done with the session, you exit the session with Exit-PSSession, and you will be returned to your local PowerShell session.  To get a list of the sessions in the current PowerShell session, you can use Get-PSSession.

Sarah and her book!!!Thanks for reading and if you missed any of the previous posts you can find a master list of series postings located here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell or on Sarah’s blog here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell. Lastly Sarah and I want to hear from you email either of us with your comments or suggestions for future postings let us know, we look forward to hearing from you. Have a great day!

p.s Sorry for the lateness of this post today, it was due to the celebration both Sarah and I had when we saw our books, I switched AM to PM and hence late today!

Comments (0)

Skip to main content