Quickly Initializing Multiple PowerShell Consoles

by Apr 27, 2021

Let’s say you are admin in many areas, i.e. Azure, SharePoint, SQL, Microsoft 365, you name it. For each environment, you may need to run some prerequisites, log onto some systems, and run some commands until your PowerShell environment is ready for action.

Here is a simple strategy that may help you spin up your different PowerShell consoles automatically. In a new folder, place a script with this content:

# safely serialize a credential
$credPath = "$PSScriptRoot\secret.xml"
$exists = Test-Path -Path $credPath
if (!$exists)
{
    $cred = Get-Credential
    $cred | Export-Clixml -Path $credPath
}

# define your different environments
$action = @{
  'Azure'  = "$PSScriptRoot\azure.ps1"
  'Teams'  = "$PSScriptRoot\teams.ps1"
  'Office' = "$PSScriptRoot\office.ps1"
}

# run a new PowerShell for each environment, and run the
# associated "spin-up" script:
$action.Keys | ForEach-Object {
  $path = $action[$_]
  Start-Process -FilePath powershell -ArgumentList "-noexit -noprofile -executionpolicy bypass -file ""$path"""
}

The sample spins up three PowerShell consoles and runs an individual startup script for each. By adding the scripts azure.ps1, teams.ps1, and office.ps1 to your folder, you can now define the code that needs to run to initialize and prepare each console. You can also use a common credential by reading the master credential from any of the other scripts. Here is an example:

# set the console title bar
$host.UI.RawUI.WindowTitle = 'Administering Office'
# read the common credential from file
$cred = Import-Clixml -Path "$PSScriptRoot\secret.xml"


Twitter This Tip! ReTweet this Tip!