Summary: The Scripting Wife talks about the first event in the 2013 Winter Scripting Games warm-up events.
Microsoft Scripting Guy, Ed Wilson, is here. Today, we have a special guest. You guessed it: The Scripting Wife is with us today. If you are reading the Hey, Scripting Guy! Blog for the first time, Welcome.
Let me give you a little background. My real-life wife, Teresa, is not an IT person—she is an accounting kind of lady. However, over the years, she has gotten involved in the Windows PowerShell community and is a tremendous asset, if I do say so myself. Teresa thought the Scripting Games sounded like fun and joined in a couple of years ago. This turned out to be a fun way to use Teresa as an example for how you too can learn Windows PowerShell. There are several blogs that we have written where Teresa has been the student, and I have conducted training scenarios with her. Now that you are up to speed I will turn the keyboard over to Teresa.
Hello everyone, thank you for joining us today. I am so happy to see the Winter Scripting Games Camp warm-up events. The Competitor Guide is available from the PowerShell.Org web site. I have completed the first scenario and wanted to share with you how I solved the scenario just in case you did not have time to complete the task.
The first task is now over, but the second task opened on February 8, 2013. Part of the purpose for these events is to test the new Scripting Games platform, as well as to give the PowerShell.Org community some first-hand experience in running the games. The Scripting Games (as you may know) has been turned over to the Windows PowerShell community. (See the announcement on the Scripting Guys blog, as well as on the PowerShell.Org website.)
The task for this first event was to display the percentage of free space (in two digits) for my disk drive.
Whenever I think about doing stuff with the disk drive, I think about using WMI. My first choice is Win32_Volume.
I did a search on the Hey, Scripting Guy! Blog and came up with this hit that talked about using format specifiers to control the way that the information displays to the Windows PowerShell console. I also found a good Hey, Scripting Guy! Blog post called Use PowerShell to Create a Report Displaying Free Disk Spacethat basically contains the entire solution to the problem. When I saw that, I decided to not look at it yet. I decided to go back and review a Scripting Wife article where I talk about creating custom table headings with Out-GridView.
After I had spent some time reviewing the articles, and making sure that I do not cheat, I decided to take a stab at it (ok, several stabs). The first part is pretty straightforward—I know I need to use Get-WmiObject to query WMI. I know I want to use the Win32_Volume WMI class. I also know that gwmi is an alias for the Get-WmiObject cmdlet, as shown here.
gwmi win32_volume
The hard part is to display the percentage of free disk space in only two decimal places. This is where the format specifiers come into play … and they do not play very nicely. If I want to format a number with two decimal places, I first define the pattern. This pattern goes inside a pair of quotation marks and inside a pair of curly brackets. The other part I really do not understand—I just copied it and it works.
Now, I do know that I use –f, which is the format operator. This is really just one of those things I need to know how to find it, so I have a pattern to do what I need to do. I can’t memorize it very well, because I do not do it all that often—but hey, as long as I can find the Scripting Guy blog and search for it, then I can find what I need.
If I want to display the number 123.00345 as two decimal places, I would use the following code.
17:37 C:\> "{0:N2}" -f 123.00345
123.00
Now, for another thing that is not very straightforward, but at least it is well-documented in Help—creating a custom table label and computed number. We know about the two percentage places, and now we need to divide and multiply to get the percent free space. I divide freespace by capacity, and multiply by 100. To get it into a custom table, I specify Label and Expression. The Label is a string that becomes the Column heading. The expression is the script block that figures out the free space and also formats the output in two decimal places. The event required a one-liner. The following is a single logical line I broke up for readability purposes.
gwmi win32_volume |
ft DriveLetter, FreeSpace, Capacity,
@{Label="PercentFree";Expression= {"{0:N2}" -f (($_.freespace / $_.capacity) * 100)}}
The one line of code and the output associated with the code are shown in the following image.
I could use the same custom table property technique to change the display of free space and capacity to display in gigabytes instead of bytes. That would be a decent amount of work, however, and it was not required by the event.
I hope you find my description useful. When the second event ends, I will publish my solution to it as well. I am not certain that my code is what they were looking for, but it does meet the requirements. Hope you have a great weekend.
Thank you, Scripting Wife—nice write up. Join me tomorrow for more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy