• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / Windows PowerShell Sessions – PSSessions

Windows PowerShell Sessions – PSSessions

16/08/2018 by Stephanos 4 Comments

PowerShell PSSessions

In this tutorial we will see about Windows PowerShell Sessions – PSSessions. We will see what are those PSSession, and how and when we need to use them. It is important to know why you need to use them. After you go through this tutorial it would be good to work with them, in order to get more familiar. After you practise you will see that it can help you a lot while working on remote computers / servers.

What is a PSSession

A session is the environment that Windows PowerShell runs. Every time that we run Windows PowerShell, a session is created. In this session we are able to run commands and functions. Also we are able to add modules, snap-ins, aliases, variables and custom items. This session that is created for us, we are not able to managed it. The items that will be created in a session a exist only in this session and they are deleted when we exit the session and the session ends.

We are able to create our own sessions, which are user-managed. These sessions are called Windows PowerShell Sessions or PSSessions. We are able to create these sessions either on local or remote computer. In a PSSession, we are able to run commands and create items, as in the default session. But unlikely the default sessions, we are able to manage the session. We are allowed to get the active PSSessions, configure them according to our needs, remove them, disconnect and reconnect to them. When we create a PSSession, it always remain available until we delete it, or it will time out. PSSessions are mostly used to run multiple commands on a remote computer. The connection that is established, when we create a PSSession with a remote computer, is persistent.

There are also temporary sessions. Those sessions we are not able to control them. temporary sessions are created to run only a single command and the sessions is closed after the command is completed. When we use Enter-PSSession and Invoke-Command along with the ComputerName parameter, Windows PowerShell will create a temporary session on the remote computer, which might be interactive as well. The session will close as soon as the command that we run is completed interactive session finishes. Any items created within the temporary session, will be lost as soon as the sessions ends.

The current session, in Windows PowerShell, is always the session that we are working on it. This might be the default session, a PSSession or a temporary session.

[adinserter name=”In Article”]

PSSessions Requirements

In order to be able to create a session to a remote computer, you must configure the remote computer for remoting in Windows PowerShell. On the remote computer, you must be a member of Administrators group or be able to provide the credentials of an administrator.

Create PSSessions

As we mentioned above, PSSessions are used when we need to run multiple commands. In order to create a PSSession, we need to use New-PSSession cmdlet. The format of the cmdlet is below:

Code:

New-PSSession -ComputerName RemoteComputer1

The above command will create a persistent session with “RemoteComputer1” in order to run multiple commands on that computer. As soon as the PSSession is created, in the output we will get the PSSession object that has been created.

 

Windows PowerShell Sessions - PSSessions - 1

 

We are able to save this object in a variable in order to use it at a later stage to reconnect on “RemoteComputer1”. In order to save the PSSession object in a variable we need to use the below:

Code:

$mysession = New-PSSession -ComputerName RemoteComputer1

 

Windows PowerShell Sessions - PSSessions - 2

 

We are also able to create sessions on multiple remote computers at once using the same command. In ComputerName parameter, we need to provide multiple computer names separated by comma ( , ). 

Code:

New-PSSession -ComputerName RemoteComputer1,RemoteComputer2,RemoteServer1,RemoteServer2

 

Windows PowerShell Sessions - PSSessions - 3

[adinserter name=”In Article”]

Get PSSessions

In order to see the PSSessions that we have created in our sessions, we need to use Get-PSSession cmdlet. By running this cmdlet alone, it provide us with the same object that we have received when we created the PSSessions.

Windows PowerShell Sessions - PSSessions - 4

If you like to refer to a specific session, you can refer to them by using:

  • PSSession ID
  • PSSession Name
  • Instance ID (a GUID)

Windows PowerShell Sessions - PSSessions - 5

You are also able to save the sessions, that are retrieve by Get-PSSession, in variables. See the screenshot below:

Windows PowerShell Sessions - PSSessions - 6

The above methods can provide you with the sessions that you have created in the current session. As from Windows PowerShell 3.0, PSSessions are maintained on the remote computer. They are completely independent of the session that they were created from. By using ComputerName parameter, in order to get PSSessions on a remote computer, it will provide you also the PSSessions from the local computer or any other computer, to the remote computer even if they were created from another session. If you are using Windows PowerShell 2.0, you will get only PSSessions that were created from your local computer in the current session.

Windows PowerShell Sessions - PSSessions - 7

As you can see above, when I used Get-PSSession without ComputerName parameter, I got two PSSessions, for the two remote servers that I have created one, in my current session. Note here that I had already another Windows PowerShell running on another window and created a session to the first server. When I run Get-PSSession command including ComputerName parameter to get only the sessions related to the first server only, I got back that there are two sessions for the particular server. It provided my also the session to that server that I had created using another session.

[adinserter name=”In Article”]

Delete PSSessions

As PSSessions create persistent connections, in order to release the resources, after the you finish you need remote the session. PSSession resources are released when the PSSession is removed or timed out. You are able to use use Remove-PSSession to delete PSSessions. Remove-PSSession allows you to specify which of the sessions to close. Similar to Get-PSSession you are able to specify the sessions by using the below methods:

  • PSSession ID
  • PSSession Name
  • Instance ID (a GUID)

By using the Session Parameter you are also to specify the PSSession object that you want to remove. Check the below screenshot to see the different ways.

Windows PowerShell Sessions - PSSessions - 8

You are also able to set a timeout limit, in order for your PSSession will be removed when it will time out. In order to specify the time out time along with other advance options for you PSSession you need to use New-PSSessionOption cmdlet. New-PSSessionOption creates an object with different options that you can specify when you are creating you session to a remote computer. You need to specify the option when you will use New-PSSession, Enter-PSSession and Invoke-Command. The below example will force our session to time out after 5 minutes of idle time. 

Code:

$MySessionOption = New-PSSessionOption -IdleTimeout 300000
$MySessionOption
New-PSSession -ComputerName RemoteServer1 -SessionOption $MySessionOption

 

Windows PowerShell Sessions - PSSessions - 9

As you can see above, I set the Idle time out limit to 5 minutes (300 000 miliseconds) and then used this option when I created my PSSession with the remote server. This session will close after the idle time out limit has been reached.

[adinserter name=”In Article”]

PSSession related cmdlets

Below is the list of PSSession related cmdlets:

  • Connect-PSSession – Connects a PSSession to the current session
  • Disconnect-PSSession – Disconnects a PSSession from the current session
  • Enter-PSSession – Starts an interactive session
  • Exit-PSSession – Ends an interactive session
  • Get-PSSession – Gets the PSSessions in the current session
  • New-PSSession – Creates a new PSSession on a local or remote computer
  • Receive-PSSession – Gets the results of commands that ran in a disconnected session
  • Remove-PSSession  – Deletes the PSSessions in the current session

You are able to get the list of related commands by using the below 2 methods:

Code:

Get-Help *-PSSession
Get-Command -Noun PSSession

 

Windows PowerShell Sessions - PSSessions - 10

[adinserter name=”In Article”]

Running Commands in a PSSession

In order to run commands in a sessions you need to use Invoke-Command cmdlet and specify the session and the commands to run. 

Code:

$MySession = New-PSSession -ComputerName bsm-rap-ad
Invoke-Command -Session $MySession -ScriptBlock {
    Get-ChildItem -Path "C:\TestFolder1\"
    Write-Host "========================================"
    Rename-Item -Path C:\TestFolder1\test2.txt -NewName FileRenamed.txt
    Get-ChildItem -Path "C:\TestFolder1\"
    Write-Host "========================================"
}

or

$MySession = New-PSSession -ComputerName bsm-rap-ad
$MyCommands = {
    Get-ChildItem "C:\TestFolder1\"
    Write-Host "========================================"
    Rename-Item -Path C:\TestFolder1\test2.txt -NewName FileRenamed.txt
    Get-ChildItem "C:\TestFolder1\"
    Write-Host "========================================"
}
Invoke-Command -Session $MySession -ScriptBlock $MyCommands

Output:

Windows PowerShell Sessions - PSSessions - 11

 

Windows PowerShell Sessions - PSSessions - 12

Note the difference in ScriptBlock parameter. When I am saving my commands in a variable, I am not using curly brackets.

I hope the tutorial about Windows PowerShell Sessions – PSSessions is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_PSSessions | Microsoft Docs
  • about_PSSession_Details | Microsoft Docs
  • Invoke-Command – Microsoft Docs
  • Enter-PSSession – Microsoft Docs
  • New-PSSession – Microsoft Docs
  • New-PSSessionOption – Microsoft Docs
  • Connect-PSSession – Microsoft Docs
  • Disconnect-PSSession – Microsoft Docs
  • Exit-PSSession – Microsoft Docs
  • Get-PSSession – Microsoft Docs
  • Receive-PSSession – Microsoft Docs
  • Remove-PSSession – Microsoft Docs
  • Get-ChildItem – Microsoft Docs
  • Rename-Item – Microsoft Docs
  • Write-Host – Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
Windows PowerShell Sessions - PSSessions
Article Name
Windows PowerShell Sessions - PSSessions
Description
Windows PowerShell Sessions - PSSessions. In this tutorial you will find information about Windows PowerShell Sessions - PSSessions and their use. Stephanos Constantinou Blog
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Connect-PSSession, Disconnect-PSSession, Enter-PSSession, Exit-PSSession, Get-ChildItem, Get-PSSession, Invoke-Command, New-PSSession, New-PSSessionOption, Receive-PSSession, Remove-PSSession, Rename-Item, Write-Host

Reader Interactions

Comments

  1. Amir Mir says

    23/08/2018 at 00:55

    This was a fantastic post. I have been using Invoke-Command but did not realize I could use it with the -Session parameter.

    Reply
  2. Amir Mir says

    23/08/2018 at 00:58

    Fantastic post. Really easy to follow and explained a lot. A few days ago I was wondering how to execute a command once I start a new session and your example of Invoke-Command with the session parameter was just what I needed.

    Always love reading your posts.

    Reply
    • Stephanos says

      23/08/2018 at 11:02

      Thank you for feedback.

      Reply

Trackbacks

  1. PowerShell PSSessions | Yogesh says:
    14/12/2018 at 05:52

    […] https://www.sconstantinou.com/windows-powershell-sessions-pssessions/ […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact