Development Powershell Tutorials

Powershell – But I only want to see this

In this post we will continue to discover basic functionality of Powershell by looking at how we can select what information we wish to display when running a Get-* cmdlet.
We will also touch on how to check what version of Powershell we are running.

Last post I talked a bit about different Powershell versions and you might wonder how you can tell what version of Powershell is running on your machine.
The answer can easily be found in a special Powershell variable called $PSVersionTable.

Open up Powershell (both the shell or ISE is fine) and type in the variable name and hit enter.

PSVersion is the one we are interested in.

Check this link for information on installing different versions of Powershell.

Now, if we instead move on to the main part of this post and to what we actually want to see.

When we used Get-Process to get all processes named ‘calculator’, we were presented with a couple of attributes.

This is fine but you might feel that some of it is a bit abundant.
Again, we have a couple of different ways we can chose what to display and we’ll look at a few of them below.

Alias = Alternative name for a Cmdlet, function or parameter. Often used to shorten how much you need to type.

This cmdlet formats the output in a table structure and displays only the attributes you choose.
However if you do not enter anything after Format-Table it will display all attributes available. In some cases you’ll need to enter the wildcard character * for all attributes to display (depends on the cmdlet).

This cmdlet is used a lot when you want to output a few attributes on the screen.
A good example for when Format-Table is perfect to use would be when you want to check all your Chrome processes and their load on CPU.

Format-Table has a very handy parameter which is called -AutoSize that auto sizes the column width.

Alias: FT (Not case sensitive)

Works like Format-Table but displays the output in a list.
Does not have the -AutoSize parameter.

Format-List is also used a lot and comes in handy when you want to display a lot of attributes for one process.

Alias: FL (Not case sensitive)

Now this one is a bit different from the other two and is used more for scripting, but still useful if all you want is to display some information on your screen.

Select-Object will output in either list or table format, based on how many attributes are present.

This cmdlet will be shown many times on this blog and is also easy to play with so try to explore it on your own too!

Some common parameters and their usage:
-First 1
Displays the first object in the list, you can enter any number to display more or less.

-Last 1
Works just like -First but instead displays the last object/objects.

-ExpandProperty Name
Only accepts one property but expands it. We’ll go into more details about this feature further down the line but it basicallys opens up the property you selects. Some properties might include several values so then you’ll see it’s sub properties.
If we take the Name property as an example, when using ‘-ExpandProperty name’ it will display the name only and not the ‘header/title’ above it.

Displays only unique values.

Alias: Select

There is more to say about all of these cmdlets and sometimes we only touch lightly on a subject, but don’t worry, we’ll be seing a lot of these in the future!

As always, continue to learn and evolve your skills, see you in the next one!

Do you want to know more? Here is a list of tutorials to check next