December 18th, 2014

Create Custom PowerShell Objects

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating custom objects via Windows PowerShell.

Hey, Scripting Guy! Question Hey, Scripting Guy! Yesterday in Understand Embedded Objects in PowerShell, you said the easy way to expand a Windows PowerShell object was to use the –ExpandProperty parameter from the Select-Object cmdlet. That works OK, but I cannot also select other properties. So I end up with a bunch of extra stuff, and I have no idea what it is related to. Is there something you can do to help with this?

—SB

Hey, Scripting Guy! Answer Hello SB,

Microsoft Scripting Guy, Ed Wilson, is here. The other day, I received an email from Windows PowerShell MVP, Richard Siddaway, confirming that I am speaking next year at Windows PowerShell Summit North America 2015 in Charlotte, North Carolina. This will be an awesome event, and there are some fantastic speakers lined up.

The cool thing is that it is in Charlotte, so it is local. for me The Microsoft office here is basically my home office. The event spaces are very nice, and the food catering will be great. One other good thing about Charlotte is that it has a nice airport and lots of direct flights, so it is easy to get to.

If you have not registered yet, you are not totally out of luck. I believe the event is sold out, but you can add your name to a waitlist.

Note  Sneak preview! The Scripting Guys holiday special this year is called Oliver Script: A Holiday Tale, and it kicks off on December 20. But the video of the accompanying holiday tune is ready to view now! Check it out: Code Glorious Code. It is quite well done, and I think you will enjoy it.

Windows PowerShell objects inside other objects

As with most stuff in Windows PowerShell, there is more than one way to do things. In fact, depending on which version of Windows PowerShell you are actually running, you will have more or fewer options available to you. The great thing about Windows PowerShell is that it keeps getting better. That is also one of the frustrating things about Windows PowerShell—depending on what blog you are reading, it may be relating old information.

For example, it is common to see scripts and blogs published (even recently), that continue to use the Windows PowerShell 1.0 way of creating objects. There is nothing wrong with that approach, except that it ends up being a lot more work than is required.

In the end, the best way to do things is the way that makes sense for you and the way that solves your particular problem. I really do not believe that there is a right or wrong way of doing things—only what works or does not work. But if I can learn a faster way of doing things, then dude, I am really happy.

Use a script to access objects in other objects

Today I want to look at using a script to access objects inside other objects. This has the advantage of being easy to understand, but it is not the best solution when working from the Windows PowerShell console window. Tomorrow, I will talk about a way to access specific properties from embedded objects in a way that works well on the Windows PowerShell console line.

One of the cool things about the Windows PowerShell ISE is that IntelliSense lists the applicable properties from embedded objects. In the following image, the StartInfo property contains an embedded object. So I access the object by first calling the StartInfo property, and then I can access the properties of that object.

Image of command output

The little script permits me to access properties from the embedded object, but it is not extremely useful. The script is shown here:

$process = Get-Process

Foreach ($p in $process)

{

 $p.Name

 $p.Id

 $p.StartInfo.Arguments

 $p.StartInfo.LoadUserProfile }

I can see the information, but that is about it. I cannot, for example, sort, filter, or otherwise work with the output. To do that, I need to create a new object.

Luckily, it is easy to create a custom object at this point. I like to use the [pscustomobject] type accelerator to convert a hash table into a custom object. I store the returned objects in the $obj variable, and then I can sort or do whatever I want to do with the returned objects. This is shown here:

$process = Get-Process

$obj = Foreach ($p in $process)

{

 [pscustomobject]@{

 Name = $p.Name

 ID = $p.Id

 Arguments = $p.StartInfo.Arguments

 LoadProfile = $p.StartInfo.LoadUserProfile } }

 $obj | Sort id

The script and its output appear in the following image:

Image of command output

SB, that is all there is to using a script to create custom Windows PowerShell objects. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

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

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.