One of the things I love doing with PowerShell is enabling my team to learn how to use it while providing tools that make specific or repetitive commands easy to use. 

Enter-PSSession is easy and straightforward to use, however it can be made even easier and used in a way to let users select from a list of servers, pick one, and then establish that connection.


The Code

function Enter-DCSession {
    $i = 0
    Write-Host "Enter PowerShell session on: ('q' exits to terminal)"`n -foregroundcolor $foregroundcolor
    foreach ($d in $ourDCs){
        if ($i -lt 10) {Write-Host "" $i -> $d.Name} else {Write-Host $i -> $d.Name}
    $enterSession = Read-Host "DC #?"
    While (!$adminCredential) { $adminCredential = Get-Credential -UserName 'domain\' -Message "Please input your admin account" }
if (($enterSession -ne "q") -and ($ourDCs.Name -contains $ourDCs[$enterSession].Name)) { 
        Write-Host "Connecting to remote session... type 'exit' to drop back to the local session."`n
        Enter-PSSession -ComputerName $ourDCs[$enterSession].Name -Credential $adminCredential
    } else {
        Write-Host `n'q or invalid option specified.' -ForegroundColor Red

You can use this function in a module, and then call it via Enter-DCSession. It will then display a list of DCs stored in $ourDCs, and allow you to connect to one of them.

The logic in the script will only accept valid name selections as it compares the returned result to the names in the array $ourDCs.

if (($enterSession -ne "q") -and ($ourDCs.Name -contains $ourDCs[$enterSession].Name))

Come to think of it, I could actually just have the latter half of that if statement an it would work the same way. There's some cleanup I'll have to do :)

This script's logic can apply to a variety of things. You have a list of objects you want to take an action on in an array as a start, and then you use a foreach to do it. Using $i = 0 before the foreach, and then $i++ within the foreach, allows you to provide users a number to interact with that matches the number in the array. 

That is how this script takes numbers as an input that matches the name in the array.

I hope you found this helpful, and I'm always open to feedback!

-Ginger Ninja