Musings of a PC

Thoughts about Windows, TV and technology in general

My next self-tutorial on PowerShell

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 ( 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 …

function prompt
   $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.


18 responses to “My next self-tutorial on PowerShell

  1. Sung July 21, 2006 at 4:48 pm

    Since I don’t have any machines available that uses batteries,
    I am not sure if the following line would cause too much time to execute or not
    > get-wmiobject Win32_Battery -property EstimatedChargeRemaining
    I wonder if people are willing to sacrifice to wait for their prompt to take long though.
    But this seems likea  great idea for those laptop users 😉

  2. Unknown April 10, 2008 at 2:25 am

    welcome to visit RF online blog Mmo blog Mmo guide blog Internet game guideTales of pirates forum ,it will give your some help.EVE online isk cheap EVE isk Guild wars gold Guild wars money GW gold Rs Money Wow gold Wow World of warcraft Gold Wow eu gold Wow us gold Maplestory mesos WOW GOLD worlf of warcrraft  World of warcraft gold Runescape RS gold RF online RF online cp RF online dalant  RF online disena  RF online money RF online gold Lotro gold Buy lotro gold  WOW gold  Cheap wow gold on sale. and you can sell your gold to us.

  3. Unknown August 16, 2008 at 3:58 pm

    Welcome to enter (wow gold) and (wow power leveling) trading site, (wow gold) are cheap, (wow power leveling) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions!

  4. Unknown August 17, 2008 at 1:23 pm

    Welcome to enter (wow gold) and (wow power leveling) trading site, (Rolex) are cheap, (World of Warcraft gold) credibility Very good! Quickly into the next single! Key words !

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: