Quick tip: Cure Excel headaches with PowerShell
- select the contributor at the end of the page -
Use PowerShell!
Then it dawned on me. Why don't I use PowerShell for this? PowerShell is awesome for working with CSV formatted files. Furthermore, if I use the import-CSV cmdlet, everything is magically transformed into an object, and the possibilities are almost unlimited for what I can do with the data.
My sample data
So, this is my sample data that I'm going to use. I'm just using a relatively small Excel spreadsheet, but that should be enough to help you understand where I'm going.
I'm going to be working with a few things like the location, OS, whether I'm dealing with a 32 or 64-bit system, and if the system is physical or virtual. I've added some other columns, but that's also just to demonstrate how to sort of skip over them when I work with the data.
Importing the data
Saving the Excel spreadsheet to a CSV is just as simple as resaving the file in CSV format, but the real magic happens when import-CSV is used to load the data. As I mentioned, the input data gets transformed into an object. As a result, I can use all kinds of built-in cmdlets to work with the data.
First off, I'm just going to import the data and save it to a variable. Then, I'm going to show the first five objects and format the output.
I'm specifically using the Format-Table cmdlet; otherwise the presentation would have been a bit more difficult to view since PowerShell was seeing a relatively large number of columns. By default it was trying to present the data in a “list format” (not shown), while I wanted to see a “table format” as shown above.
I'm specifically using the Format-Table cmdlet; otherwise the presentation would have been a bit more difficult to view since PowerShell was seeing a relatively large number of columns. By default it was trying to present the data in a “list format” (not shown), while I wanted to see a “table format” as shown above.
Working with the data
Now, let's run through a few typical scenarios and then show the code:
- Scenario 1: I know my data isn't complete. Show me the hostname and rack for systems where I don't have a location listed.
- Scenario 2: I need to move my servers in location “loc2”, list the number of servers from that location, and the rack.
- Scenario 3: Similarly to scenario 2, notice that I have virtual machines listed in some racks; these must be in a physical host; show me the listing of physical and virtual servers in location “loc1”.
- Scenario 4: My network team needs to make some network changes and want a list of hosts by VLAN.
- Scenario 5: I want a breakdown of servers by OS and 32/64-bit.
The full code is shown at the end of this article. In the screenshots below, I've used aliases and other shortcuts to try to shorten the code quite a bit.
Below, I'm showing the code for the first three scenarios. I'm not going to walk through each code example, but by matching the scenarios listed above, I hope readers can follow the logic.
Scenario 3
I will briefly talk about scenario 3. I might have been able to go through some more complicated logic to try to better represent the data, but I chose instead to sort of group the “rack” and “physical/virtual” property together as shown.
Full code:
$data|where-object{$_.location -eq "loc1"}|select-object -property location,rack,'phy/virt'|group-object -property rack,'phy/virt'|select-object -property name,count|format-table -autosize
Scenario 4:
Below, I show my first attempt to script scenario 4, which is a bit difficult to read, so I used “calculated properties” in my second try to present the information in a slightly more readable format.
Full code:
$data|select-object -property server,vlan|group-object -property vlan
Other parts of the Office Suite
There are also opportunities to work with other products in the Office Suite, like Word. Be on the lookout for more examples of working with some of the other products in the suite in a future post.
$data|where-object{$_.location -eq ""}|format-table -autosize
$data|where-object{$_.location -eq "loc2"}|select-object -property location,rack|group-object -property rack|select-object -property name,count|format-table -autosize
$data|select-object -property os,'32/64'|group-object -property os,'32/64'|select-object -property name,count|sort-object -property name|format-table -autosize
Ready to test your skills in Powershell? See how they stack up with this assessment from Smarterer. Start this Powershell test now.