Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about pinning a Windows PowerShell script to the Windows 8.1 Start screen.
Microsoft Scripting Guy, Ed Wilson, is here. The other day, the Scripting Wife and I had a couple of neighbors over for dinner. While munching on snacks, he said that he had recently upgraded his laptop from Windows 7 to Windows 8. His initial impression was that Windows 7 was for work, but Windows 8 was for play, and he needs to use his laptop to accomplish work. He said, “Everything is at least five or six clicks away.”
I told him he was trying too hard, and I took him to my office to give a short demonstration. With my multiple monitor configuration, I have the Windows 8.1 Start screen on one monitor, my Word documents on another monitor, and the Windows PowerShell ISE on the third monitor. It makes life really easy. I love the Start screen because everything is a single click away, not five or six clicks. The trick is to know how to configure your Start screen, and how to add or take away what you want to use.
I have a Surface 2 Pro and a Surface RT that take turns following me around the house like little puppy dogs. They are great and helpful, and I do not know how I ever got along without them. But this usefulness comes at a price. There is no device that will run for 18 continuous hours of operation. Therefore, I have two devices, and I swap them out.
I, of course, use the Surface 2 Pro for things that I need it for, such as using Live Writer to write blog posts, or to upload pictures from my camera into my image editing software program. Yep, it is a great device, but I need to watch my battery life. I can swipe from the right side of the screen, click Properties, and get a battery status. This is shown in the image that follows:
The icon looks full, but is it really 100% topped off? Or is it 89% left? When exactly, does the icon change?
I can easily find out how much juice is left by using Windows PowerShell. All I need to do is to use the Get-CimInstance cmdlet and return information from Win32_Battery. Because I only want the information, I use the GCIM alias as shown here:
The output is shown in the following image:
The output is clear. I have 97% of my battery time left, and an estimated running time of 189 minutes. Cool. I should be able to finish my blog post before the battery goes on holiday. The command is easy enough to type, and I have been doing it consistently for over a year. I absolutely love that I can use Windows PowerShell on the Surface.
But, it is still a decent amount work to open the Windows PowerShell console and type my command simply to see the output. Wouldn’t it be easier if I could combine my love of the Start screen and my love of Windows PowerShell? Well, as it turns out I can.
I decided to create a shortcut to my Windows PowerShell script, in which I use the arguments of the PowerShell.exe command to retrieve my battery-life information. I place the shortcut in the Programs special folder, and I create a .lnk file as shown here:
Param($scriptFile = "c:\fso\get-battery.ps1",
$Description = "Created by script",
$WorkingDirectory = "C:\")
$arguments = " -noexit -file $scriptFile -ExecutionPolicy bypass"
$program = "PowerShell.exe"
$linkname = Split-Path -Leaf -Path $scriptFile
$startPage = $wshShell.SpecialFolders.Item("Programs")
$shortcut = $wshShell.CreateShortcut("$startPage\$linkname.lnk")
$Shortcut.TargetPath = $program
$Shortcut.description = $description
$ShortCut.arguments = $arguments
$shortcut.WorkingDirectory = $workingdirectory
For my purposes, all I need to do is to change the location of the script that I want a shortcut to. My Get-Battery.ps1 script? Well, it is shown here:
There is no script editor on the Surface RT, but that does not stop me from creating scripts by using Notepad. In fact, I used Notepad for years to write scripts before Windows PowerShell 2.0 came out with an editor. All I need to do is to save the script file, and then I can use my previous function to store a shortcut to the script within the Programs special folder.
The arguments that I specified for PowerShell.exe include –noexit. I do this so I can see the output from the script. It is sort of the point of the exercise. I also specify the path to the file, and I specify an –executionpolicy of bypass. In this way, I do not need to worry about the script execution policy on my device. When I run the function, the shortcut is placed in the Programs special folder.
Now, I need to restart my device, so that the Start screen will reload. When this is complete, I can use Windows PowerShell to find the script, as shown here:
PS C:\Users\ed> Get-StartApps | ? name -like 'g*'
Notice that the shortcut to my script, is autogenerated. Cool stuff.
Where else can I find my shortcut? It is in the Apps location as shown here:
Now, all I need to do is to right-click the shortcut on the App page, and click Pin to Start. This is shown here.
After I do that, it shows up on my Start screen, and I can put the shortcut anywhere I want it. Here, I have moved it to a handy location—directly under my games.
When I click the shortcut tile, it bounces to the desktop, opens the Windows PowerShell console, and presents the information I want. Because I specified –noprofile, the prompt is a generic black Windows PowerShell prompt—but hey, it works. This is shown here:
Well, I am down to 78 percent on my battery now, but that is still 155 minutes remaining. So I am good to go.
Join me tomorrow, when I have a guest blog post about DHCP written by Microsoft PFE, Ian Farr. It is an excellent two-part series, and you will not want to miss it.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
The New-Shortcut function above will fail because $WshShell is not initialized. Add the following line:
$WshShell = New-Object -ComObject WScript.Shell