Powershell and Objects

PowerShell is .NET based.  .NET is object orientated. Therefore PowerShell is object based.  One of the first things we learn about PowerShell is that we are working with .NET objects.  Just what does that mean?

Look at the list of cmdlets specifically designed to work directly with objects:

Compare-Object
ForEach-Object
Group-Object
Measure-Object
New-Object
Select-Object
Sort-Object
Tee-Object
Where-Object

Commonly the aliases foreach, group, select, sort and where are used for their respective cmdlets.  These are the ones that are used all of the time and everyone is happy with.  What about the others? When was the last time you used Tee-Object?

Tee-Object enables you to store output in  a file or variable and display to the console (if tee-object terminates pipeline) or pass along the pipeline

Consider

Get-Process | Tee-Object -FilePath ptest.txt | Select name, handles

Get-Content ptest.txt

Will get the current processes, save the information in a file and then display the name and handles of the processes.  We can then view the contents of the file.

Even more useful is this

Get-Process | Tee-Object -Variable procs | select name, handles
$procs
$procs | select name, handles

This time we put the information into a variable.  Note we don’t use a $ on the variable name.

We can then input $procs to the pipeline for further analysis.

Any time you may need to dig further into some data think about using tee-object in your pipeline.  Could save a bit of effort.

 


Share this post :

 

Technorati Tags: ,

About these ads
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s