After a big, long break from blogging on PowerShell, I finally found the time and the incentive to do some more learning on how to write in PowerShell. The incentive was a combination of three things:
- I’ve been trying to think of what to actually use PS for, and how to learn how to program in it. One of the ideas that has been going through my mind is taking some of the Script Centre scripts and re-writing them in PS.
- A recent blog from Jeffrey Snover on setting the console title to be the current working directory. I’m glad he blogged about this ‘cos I saw his demos at Tech-Ed and I’d been wondering how he had done that neat trick.
- A link from Jeffrey’s blog entry to a blog entry on the PowerShell for Fun site that goes into more detail on what you can do with setting the prompt. That entry ends with some suggestions on what the reader can do next …
So where did that leave me? I ended up wanting to write a prompt function that would warn me if the battery was running low. So, over to Script Centre first to see if I can find an existing script on getting battery information. Yep, there are a couple of scripts there, of which one (http://www.microsoft.com/technet/scriptcenter/scripts/hardware/power/hwpwvb01.mspx) deals with the Win32_Battery object.
I tried running that script on my laptop and it didn’t really give me much information that might have been useful. I checked MSDN about Win32_Battery but that didn’t really get me advanced on what I was trying to achieve. What I did find, though, was an article on using WMI with PowerShell, written by Andrew Barr at Microsoft:
This helps a lot. If you type get-wmiobject Win32_Battery then you get all of the properties that WMI can return from this. Looking through the list, I spotted EstimatedChargeRemaining … aha! I could write a prompt function that retrieves the estimated charge remaining and sets the prompt to be a different colour as the charge percentage goes down …
$charge = get-wmiobject Win32_Battery -property EstimatedChargeRemaining
$colour = "White"
if ($charge.EstimatedChargeRemaining -lt 50)
$colour = "Yellow"
if ($charge.EstimatedChargeRemaining -lt 25)
$colour = "Red"
Write-Host ("PS " + $(get-location) + ">") -nonewline -foregroundcolor $colour
return " "
So if the battery charge drops below 50%, the prompt text should turn yellow. If it continues to fall and goes below 25%, it should go red.
A couple of things that caught me out briefly …
1. You must return something from the prompt function. In the example above, I’m returning a space. To begin with, I returned "" (i.e. an empty string) but PowerShell then issues the default prompt!
2. Being English, I accidentally started off trying to specify -foregroundcolour (i.e. with a u) … and then wondered why -foregroundcolour White was being issued as part of my prompt!
The PowerShell for Fun blog entry linked to above has a brief but useful reminder as to where PowerShell will gets its config files from when it starts up. If you want this function to be persistent, you need to add it to one of those files. The one complication here is that if you’ve only just started to use PowerShell, the script execution policy may be set to restricted. If that is the case then, when you start PowerShell having just added the prompt function to one of the startup files, you may see this error:
The file profile.ps1 cannot be loaded. The execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:2
+ . <<<< profile.ps1
If you type get-executionpolicy, you can find out what your machine is set to. Mine was set to restricted. I wanted to change it to RemoteSigned (so that local scripts can be run unsigned but downloaded ones must be signed) but my first attempt failed because I don’t have admin rights so the write to the registry failed.
Running PowerShell as an admin account and then typing set-executionpolicy RemoteSigned then allowed my format prompt to be set.