Post

Windows PowerShell Console Fun

Whilst watching Die Hard 4 the other day I noticed the funky transparent console windows that were being used to battle out cyber warfare, and being a traditional geek I immediately liked the idea of doing the same for my PowerShell console. Sure I know these guys are using Linux and transparent consoles have been around for years but still I fancied some of that slickness on my Windows.

It didn’t take long to find PSGlass on CodePlex (http://powershellglass.codeplex.com/) which is a neat little exe that runs in your system tray and hunts out any PowerShell console windows, and any it finds it converts to transparent using the Windows Aero effects. The peek into the source code shows that it’s checking for a window with a process name of “powershell” or “cmd” and then uses the DwmEnableBlurBehindWindow API to make it transparent.  The effect is shown on the left. It’s simple and effective and could be extended easily to do more. Not content with this I wanted to achieve the same result from within PowerShell itself so set about using the API in a script. Luckily for me Oisin Grehan had already written a script to achieve the same result and posted it on PoshCode.org, check it out at http://poshcode.org/2052. For this script the DwmExtendFrameIntoClientArea API is used to create a sheet of glass effect with no borders and the effect is much more striking (right screen shot). The fact that you don’t have to have an application running in the background is of course much better and as its a script you can add it to your profile to always take effect on Powershell start-up. I have found it useful to create a function in my profile to toggle the glass effect on/off depending on my mood and what actions I am trying to perform.

As the console was now transparent I quickly found myself wanting it to stay on the top of other windows and so set about looking for a script for that too. Again the excellent PoshCode.org came to the rescue with this script from Shay Levy at http://poshcode.org/1837. Again I downloaded the script and added a function to my profile to be able to toggle it on/off at will from within the console itself. In order to access the Set-TopMost() function that sits within the TopMost.ps1 script I used dot sourcing (described here by Jesse Hamrick) and my functions are shown below:

1
2
3
4
5
6
7
8
9
10
11
function OnTop  
{  
  . TopMost.ps1  
  Set-TopMost (get-process -id $pid).mainwindowhandle  
}

function NoOnTop  
{  
  . TopMost.ps1  
  Set-TopMost (get-process -id $pid).mainwindowhandle -disable  
}

For total geek heaven why not go the extra mile and put a Matrix style screensaver within the console itself. If you haven’t seen it I would recommend checking it out at http://www.nivot.org... The video on the site shows how it works but it essentially runs a Matrix code screensaver inside the console (not the whole desktop).  Now that’s one clever PowerShell script!

2017 UPDATE:

As PoshCode.org site has been down a while I’ve added alternative links to be able to find the above mentioned scripts from the awesome web.archive.org instead of PoshCode.org:

This post is licensed under CC BY 4.0 by the author.