Summary: Learn how to sort data before you send it to the Windows PowerShell Out-GridView cmdlet.
Microsoft Scripting Guy, Ed Wilson here. The weekend has finally arrived, following on the heels of a rather hectic week. I decided to download and install Service Pack 1 for Windows 7 and Windows Server 2008 R2. It installed on the Scripting Wife’s computer just fine. On my workstation and on my laptop, it failed. (This actually made me a little happy because the Scripting Wife had to perform her own upgrade, and I was glad it did not fail on her computer.)
It seems that adding an extra hard disk drive after installing Windows 7 caused some confusion, and I had to use DiskPart to cause it to automount my second hard disk drive. This is due to the need to update the boot configuration data (BCD) during installation. After updating various drivers and flashing the BIOS, I disabled Microsoft Security Essentials. (While I was at it, I uninstalled the Remote Server Administration Tools (RSAT) tools for good measure.)
These last two steps were based on information I read in the TechNet forums. Anyway, after spending an entire day performing what I had figured would be about an hour-long operation, I was suddenly very behind for the remainder of the week. But hey, the remaining upgrades of my servers went flawlessly, and in the end, I actually got a minor performance boost on my laptop when I ran the Windows Experience Index assessment (that may have been due to the updated drivers, but I will take what I can get.)
I am drinking a cup of Chamomile tea and looking forward to spending a quiet day reading my new John Buchan book. I was just settling down to read The 39 Steps when I heard a sort of thumping sound coming from the door. I glanced up, but no one was there. I looked back down at my book, and within minutes, the thumping sound was once again audible. This time I got up, and walked toward the living room. Slowly I turned—inch-by-inch, step-by-step, halting only to listen for imperceptible sounds that might indicate the presence of alien entities. I saw nothing. Upon re-entering the reading room, I immediately saw that my seat was taken, my book placed on the floor, and a radiant face was beaming with obvious joy.
“Fooled you, huh?” the Scripting Wife said.
“Why yes, you did. I had no idea what was making that noise,” I feigned.
“You know what?” she asked.
“What.”
“There is something you forgot to tell me about the Windows PowerShell Out-GridView cmdlet.”
“And what might that be,” I patiently asked.
“Well for one thing, you need to show me how to sort stuff before it gets to the cmdlet.”
“And?”
“And you need to show me how to remove columns, or how to rearrange columns,” she said.
“OK. That makes sense. Open the Windows PowerShell console. Now use Get-Process to get a list of processes,” I instructed.
She clicked the Windows PowerShell icon on her task bar, and seconds later, she was typing. She typed the following command into the Windows PowerShell console.
Get-Process
The command and the associated output are shown here.
“Now I want you to send the output of the Get-Process cmdlet to the Get-Member cmdlet. You will need this so that you can tell what the actual property names are,” I stated.
She thought for a second and then typed the following command.
Get-Process | Get-Member
The exact key strokes that she typed are shown here.
Get-Pr<tab><space>|<space>Get-m<tab><enter>
The command and associated output are shown in the following image.
“That is sort of cool, but why did you tell me to do that?” she asked.
“Look at the names that are listed in the Property section at the bottom of the page. Those are the names you need to use in your query,” I said.
“Yes.”
“It means that the property was added by the Windows PowerShell team. But for our purposes, it is like any other property. Let’s see what happens if we sort on CPU,” I said.
“OK. What do I need to do?” she asked.
“Pipe the results from the Get-Process cmdlet to the Sort-Object cmdlet, and choose the CPU property,” I said.
The Scripting Wife thought for a few seconds, and then slowly began to type. The command she composed is shown here.
Get-Process | Sort-Object cpu
The keys she typed are shown here.
Get-Pr<tab><space>|<space>Sort-<tab><space>cpu<enter>
The command and associated output are shown in the following image.
“But why is everything down at the bottom? Why do most of the processes not have any CPU time?” she asked.
“Well, you actually ask two questions. The first answer is that you are not running the Windows PowerShell console with administrator rights, and therefore you do not see the CPU time of the system processes. The second answer is easier—nothing beats something. If you want all your CPU times to appear at the top of the list, you will need to change the sort direction. Use the Descending switch,” I said.
She clicked the Up Arrow to retrieve the previous command, and then added the Descending switch parameter. Her command is shown here.
Get-Process | Sort-Object cpu -Descending
The exact key strokes the Scripting Wife typed are shown here.
<up arrow><space>-d<tab><enter>
The command and associated output are shown in the following image.
“Now you are ready to send the whole thing to the Out-GridView cmdlet,” I announced.
She once again recalled the previous command by using the Up Arrow, added a pipe character, and typed Out-GridView. The command is shown here.
Get-Process | Sort-Object cpu -Descending | Out-GridView
The exact keystrokes she typed are shown here.
<up arrow><space>|<space>out-g<tab><enter>
The Out-GridView cmdlet does not return anything to the Windows PowerShell console. Instead, the grid view appears as shown in the following image.
“If you want to remove columns from the grid view, you right-click the column process names, and then click Select Columns,” I said.
The column headings and the Select Columns prompt are shown in the following image.
“When the Select Columns prompt appears, click it to bring up the Selected columns dialogue box,” I said. “Unfortunately there is no Select all or Deselect all button. You have to click to add or remove the columns individually.”
The Selected columns dialog box is shown in the following image.
The resultant grid view is shown in the following image.
“That is pretty wild,” she said. “And I can still play with the columns to sort as well.”
“Yep. Keep in mind, that because selecting columns is a bit crude, if you only want to see a few columns, it is better to filter the columns before you send it to Out-GridView. Here let me show you,” I said.
I took the computer, recalled the previous command, and edited it until the command shown here appeared.
Get-Process | Sort-Object cpu -Descending | select cpu, name | Out-GridView
“I see. That is much faster, than all that clicking in the Selected columns dialog box,” she said. “I will let you get back to your book. See ya.”
She walked out of the room, like a breeze that spreads leaves across a neglected fall lawn. I even thought I heard a rustling as she walked. Suddenly John Buchan seemed somehow tame.
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
0 comments