Powershell versions, write-host and colour coding tables!


My nice shiny script that took me ages falls apart when I'm running it in Windows 10. I'm assuming because the PC that it is running on have a higher version of powershell.

Here's the original script. Its supposed to make a nice colour coded table of users in Office 365 with the colour depending on the department field, followed by a list of users with no department, followed by a list of users whose name begins with "ZZZ".....


# connect to O365 Import-Module MSOnline write-host "Enter the Office 365 Admin username and password" -ForegroundColor Green write-host "`n`n`n`n" Connect-MSOLService #-------------------USERS--------------------------- write-host "---------- Users ----------" -BackgroundColor DarkGreen $Users = get-msoluser | where {$_.displayname -NotLike "ZZZ*" -and ($_.department -eq "Contractor" -or $_.department -eq "AD User" -or $_.department -eq "Syndicated" -or $_.department -eq "System")} | Sort DisplayName #Get the original colsole colour to reset it back later $originalColor = $Host.UI.RawUI.BackgroundColor $Users | Format-Table -Property displayname, @{label = "Department" ; Expression = { if ($_.department -eq "Contractor") { $Host.ui.rawui.BackgroundColor = "DarkGreen" ; $_.department } if ($_.department -eq "AD User") { $Host.ui.rawui.BackgroundColor = "DarkGray" ; $_.department } if ($_.department -eq "Syndicated") { $Host.ui.rawui.BackgroundColor = "Red" ; $_.department } if ($_.department -eq "System") { $Host.ui.rawui.BackgroundColor = "Magenta" ; $_.department } } #end expression } #end hash #set the console colour back to the original $Host.UI.RawUI.BackgroundColor = $originalColor #-------------------ANOMALIES--------------------------- #List active users with no department set. $Anomalies = get-msoluser | where {$_.displayname -NotLike "ZZZ*" -and $_.department -ne "Contractor" -and $_.department -ne "AD User" -and $_.department -ne "Syndicated" -and $_.department -ne "System"} | select DisplayName, Department, UserPrincipalName, isLicensed | Sort DisplayName $anomaliesM = $Anomalies | measure $anomaliesM.count write-host "`n`n`n`n----------" $anomaliesM.count " Anomalies ----------" -BackgroundColor Cyan write-host "Active Users with no department set in Office 365`n" $Anomalies #-------------------LEAVERS--------------------------- write-host "`n`n`n`n---------- Leavers ----------" -BackgroundColor Red write-host "Office 365 users that have been set to 'ZZZ - ...'`n" get-msoluser | where {$_.displayname -Like "ZZZ*"} | select DisplayName, Department, UserPrincipalName, isLicensed | Sort DisplayName pause


...when I run this on on a Windows 10 PC then it seems to break at each "write-host" with all of the write-host printing first then the rest of the outputs coming after! I can correct this by using write-output instead but this looses my nice formatting.

It also loses all of the colour coding in the table!

The computer it works on returns $psversiontable.psversion as:

Major: 4

Minor: 0

Build: -1

Revision: -1

The computer it doesn't work on:

Major: 5

Minor: 1

Build: 14393

Revision: 1715

..................................does anyone have a clue why it breaks so badly between versions?

Also how do I post code so that it doesn't look a hideous mess?


Many thanks for any pointers.