Error: Twitter did not respond. Please wait a few minutes and refresh this page.
Thoughts about Windows, TV and technology in general
Windows tries to protect you from yourself, and it tries to do it without nannying you, but sometimes it can be a real nuisance. A good example is blocked files. If you download something from the Internet, it is considered to be “untrusted” and therefore blocked from being used until you go and unblock it.
Which is all well and good if it is just one file, but if you’ve downloaded a Zip file of source and you extract everything from the Zip file, you’ve then got to unblock each and every file.
Which is a nuisance if you are using Explorer, because you can only do one at a time.
So it was really nice to find that PowerShell can unblock files and you can provide it with a recursive listing of files to unblock, e.g.:
Get-ChildItem -Recurse | Unblock-File
USE WITH CAUTION! This will literally unblock every file from the current directory downwards, so be sure that you do trust everything you are unblocking, but it should save you some time.
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.