Creating multiple PSSession sessions incrementally

Hi All,

I am in the process of creating a dashboard that runs a script to create a new user.

The script will clear any existing sessions, set the session value to 0 and then create a new session. When the user creation task occurs and once the account has been completed, the session is disconnected and removed.

So that a session remains active whilst in use and doesn’t get disconnected when a new session is executed, how can i perform a check to see if a session exists and if it does create a new one with the next ID value.

e.g. admin A opens sessions and starts creating a new user

admin B logs on straight after on a different machine and starts to create a new user also

i would like each admin to have their own session without closing each others session.

here is an example of my script:``

$session = $null
If($session -eq $null){
$session = 0

Else {$session++}

Disconnect-PSSession $session

Remove-PSSession $session

Will this suffice? Am I going about this the right way?

Thanks for your feedback.


If you have a cmdlet New-PSSession and Remove-PSSession there’s likely a cmdlet Get-PSSession to list the existing PSSessions.

You should read the complete help for each cmdlet you’re about to use … including the examples to learn how to use it.

BTW: You can re-connect sessions if you need, you can name them to identify them and you can ‘save’ sessions in a variable for later use.