Output

Any output from any PowerShell cmdlet or script is sent to the pipeline, we will look at this in more detail in the next section, the pipeline has some default settings and so if you don’t tell PowerShell where to send what’s on the pipeline it will use it’s default settings.

The default output is normally your screen or console.

PowerShell also does not output text. It may look that way but what it actually puts on the pipeline are objects. An object is just a data structure that consists of one or more things. You can think of it in terms of a database record with a number of fields and values. Your output could be more than one record, a collection of records if you like. In PowerShell they refer to it simply as a collection.

What does this look like in a PowerShell and how do we know what kind of object is returned? Well we got back to our old friend Get-Member

The first thing we see returned from the Get-Member output is the type of object the command returns

PowerShell console showing a cropped output of Get-Process piped to Get-Member showing the returned object type

Then we get all the “field names” and “datatypes” just like we would have in a database for each record, or maybe for each row in a spreadsheet, I couldn’t fit them all in one screen so here is a sample?:

Powershell console showing cropped output of Get-Process when piped into Get-Member. It lists some of the members of Get-Process

The Get-Process cmdlet has 92 different methods, properties, sets or aliases in its object type System.Diagnostics.Process

An Image showing the summation of member types in object System.Diagnostics.Process

OK, so an object can have one or more parts to it what are those parts?

  • Property – This is what holds the value, the actual data we want.
  • Method – An action that can be performed on the object
  • ScriptProperty – these are used to calculate property values
  • AliasProperty – As its names suggest, it is an alias to another property
  • Event – These are .NET events linked to the object.
  • PropertySet – Unsurprisingly, it holds a set of properties.
  • NoteProperty – Simply hold static property names

Let’s look at these in a bit more detail

Property

It holds some data of a predefined type. Property data types are often things like strings, integers, boolean or datetime and they are the data or values that we normally want to see when we run our cmdlet. Name, state, version are all examples of a property

Method

This is something we don’t use much as we start out with PowerShell, but you will find you use more as you become more familiar with them.

Let’s look at the output of Get-Member where we filter only the Methods. there are some that should be obvious, kill, close, refresh, start. These are all called by adding them to the end of the cmdlet

Image of PowerShell console showing members of Get-Process with a MemberType of Method

Try this practical example:

  1. Start notepad
  2. Open PowerShell
  3. Run the command get-process -name notepad
  4. Review the output, you should see the notepad process
  5. Run the command (get-process -name notepad).kill()
  6. Run the command get-process -name notepad again

The notepad process no longer exists, you used the kill method to stop it from running, the image below shows this in my console:

Image of a PowerShell Console showing the process details for a process named Notepad

Alias

Those used to BASH will understand the concept of aliases. If you haven’t come across them before they are simply an alternate name for an existing thing often a command or property. PowerShell has a number of built in aliases for the lazy amongst us, we discussed some in the last lesson.

  • Select is an alias for Select-Object  
  • Where is an alias for Where-Object

There are other as well, ls and dir are both aliases for Get-Childitem

In an object the alias property usually points to an other property. We can investigate this in our Get-Process command by looking at what they point to. Call Get-Member again but this time look at the member type aliasproperty

Image of a PowerShell console shooing the member of Get-Process with a MemberType of AliasProperty

Lets look at the first couple of these:

  • Handles is an alias for HandleCount
  • Name is an alias for ProcessName

this simply means we can get the same output using the shorter aliases

Image of a PowerShell console showing get-process using alias members and property members producing the same result

Summary

Output from any PowerShell cmdlet or script is sent to the pipeline, if you don’t specify differently, it is eventually sent to the default output devices which is normally the console

Output is in the form of objects

Objects are collections of members (these are more commonly referred to as properties but this can get confusing)

Members have different types. Method, Property and Alias are the ones you’ll use the most.

Further Reading

Microsoft Docs – About Object

Microsoft Docs – About Methods

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.