Customizing the Windows PowerShell Console


Another blog on customizing the PowerShell console?  Really?

Yes.  This is actually what I use on a day-to-day basis as a consultant managing many projects at once.  I find that I’m usually running various PowerShell sessions concurrently and having to keep which one is which can be difficult without having to stop and check which server or Office 365 tenant I’m connect to, or what type of activity I’m performing.

To help keep this straight, I put a few functions in my PowerShell profile for my day-to-day use.

To edit your PowerShell Profile, you can type notepad $PROFILE from a PowerShell prompt.  If you’ve never modified or created one, you’ll need to create the directory first opening a PowerShell prompt, typing New-Item -Path $Profile -Type File -Force and pressing Enter.

My PowerShell Profile looks like this:

Import-Module MSOnline
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
#
# Set Console Settings
Function Console($Background,$ForeGround,$Title)
        {
        Clear-Host
        $console = $host.UI.RawUI
        $console.BackGroundColor = $Background
        $console.ForeGroundColor = $Foreground
 $console.WindowTitle = $Title
        cls
        }
#
# Check IP
Function CheckIP
	{
 	$ip = (Invoke-WebRequest -Uri http://checkip.dyndns.com).content -replace '[^\d\.]'
 	Write-Host -NoNewLine "External IP is ";Write-Host -ForegroundColor Green "$ip"
	 }
#
# Color-coded logs
Function LogColor 
	{
     	Param([Parameter(Position=0)]
     	[String]$LogEntry)
	process {
         	if ($LogEntry.Contains("DEBUG"))
   			{ Return "Green" } 
        	elseif ($LogEntry.Contains("WARN"))
   			{ Return "Yellow"}
  		elseif ($LogEntry.Contains("VERBOSE"))
   			{ Return "Cyan" }
        	elseif ($LogEntry.Contains("ERROR"))
   			{ Return "Red" }
        	else
   			{ Return "White" }
      		}
 	}
#
# Unix tail-like function with color coding
Function Tail($File,$Length)
	{
	Get-Content $File -Tail $Length -Wait | ForEach {Write-Host -ForegroundColor (LogColor $_) $_}
 	}
# Office 365 Functions
#
# Office 365 Logon
Function o365Logon([switch]$Lync)
 	{
 	$UserCredential = Get-Credential
 	$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
 	Import-PSSession $Session
 	Connect-MsolService -Credential $UserCredential
 	If ($Lync)
  		{
  		$LyncSession = New-CSOnlineSession -Credential $UserCredential
  		Import-PSSession $LyncSession
  		}
 	}
#
# Get basic Office 365 license assignment for a user
Function GetLicenseBasic() 
 	{ 
 	(Get-MsolUser -UserPrincipalName $args[0]).Licenses.ServiceStatus 
 	}
# Get detailed Office 365 licenses assignement for a user
Function GetLicense()
 	{
 	$User = Get-MsolUser -UserPrincipalName $args[0]
 	Foreach ($lic in $user.Licenses) 
  		{ 
  		$lic.AccountSkuId
  		$lic.ServiceStatus
  		write-host =======================================================`n
  		}
 	}
#

What the main functions do:

The function Console($Background,$Foreground,$Title) is a function declaration that allows me to easily change the foreground, background, and window title of the current PowerShell window.  To change and of those settings, after launching my PowerShell window with this profile, I simply type Console -ForegroundColor <color> -BackgroundColor <color> -Title <what I want in the title bar>.

The CheckIP function returns your external IP address.

The functions LogColor and Tail are designed to work together.  LogColor searches text and highlights rows based on whether they contain cert rows; it’s called by the Tail function (which emulates Unix’s tail -f), allowing you to see log coloring in real-time.

The function o365Logon calls the a series of commands to connect to an Office 365 tenant (prompting for credentials, importing Exchange and Lync/Skype sessions).

The functions GetLicense and GetLicenseBasic return the licensing status for an Office 365 user.  GetLicense shows that SKU assignments for a user including which service plans are enabled or disabled, while GetLicenseBasic returns only the SKU.

Comments (0)

Skip to main content