PowerShell

Creating Custom Objects in PowerShell: v3.0 Enhancements

We’re at the end of our multi-part series on creating custom objects in PowerShell! In part one, I discussed the basics of creating custom objects in PowerShell to meet your needs; however, in this version, we lost the original object. In part two, I showed you how to modify and create the custom object while retaining the original. In part three, we learned how to create a custom object from scratch.

Everything I’ve demonstrated up to now is intended for PowerShell 2.0. My examples should also work in the upcoming PowerShell 3.0, but v3 offers some terrific enhancements. Today, I’ll go deeper and include the enhancements found in PowerShell 3.0.

First, instead of using New-Object and passing a hash table of property values, we can easily create an object with the [pscustomobject] type accelerator.

​PS C:\> $obj=[pscustomobject]@{

>> Name="Jeff Hicks"

>> Title="PowerShell Pro"

>> Help="http://bit.ly/AskJeffHicks"

>> Blog="http://jdhitsolutions.com/blog"

>> }

>>

 

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

I’m still creating a hash table, but in front I’m using the type accelerator. The end result is a custom object.

​PS C:\> $obj | format-list

Name  : Jeff Hicks

Title : PowerShell Pro

Help  : http://bit.ly/AskJeffHicks

Blog  : http://jdhitsolutions.com/blog

 

But it gets better. If you recall from a previous article, there was a subtle issue when using hash tables for property values: you couldn’t control the order. That is still true in PowerShell 3.0. Here’s a simple hash table.

​$drivec=Get-WmiObject win32_logicaldisk -filter "deviceid='C:'"

[email protected]{

Computername=$env:computername

ProcCount=(Get-Process).Count

OS=(Get-WmiObject Win32_operatingsystem).Caption

C_Size=$drivec.size

C_Free=$drivec.freespace

}

 

When I write it to the pipeline as a custom object I get this:

​PS C:\> [pscustomobject]$hash

Computername : CLIENT8

C_Free       : 9823666176

ProcCount    : 87

OS           : Microsoft Windows 8 Pro

C_Size       : 26474442752


Not easy to read and not what I want. But now, we can use another new attribute called [ordered] when defining the hash table.

​$hash=[ordered]@{

Computername=$env:computername

ProcCount=(Get-Process).Count

OS=(Get-WmiObject Win32_operatingsystem).Caption

C_Size=$drivec.size

C_Free=$drivec.freespace

}

 

Now, the table order is maintained both as a hash table and when turned into an object.

​PS C:\> $hash

Name                           Value

----                           -----

Computername                   CLIENT8

ProcCount                      87

OS                             Microsoft Windows 8 Pro

C_Size                         26474442752

C_Free                         9823666176

PS C:\> [pscustomobject]$hash

Computername : CLIENT8

ProcCount    : 87

OS           : Microsoft Windows 8 Pro

C_Size       : 26474442752

C_Free       : 9823666176

 

I love this. Now it is even easier to create rich objects that can live in the pipeline without complicated Select-Object statements or formatting extension files.

​Get-content computers.txt | foreach {

#get some data

$os=get-wmiobject Win32_operatingsystem -ComputerName $_

$process=Get-WmiObject win32_process -ComputerName $_

$psversion=invoke-command {$psversiontable.psversion -as [string]} -ComputerName $_

#build an ordered hash table

$hash=[ordered]@{

Computername=$os.csname

OS=$os.caption

OSArchitecture=$os.osarchitecture

ProcessCount=$process.count

PSVersion=$psversion

}

#write the object to the pipeline

[pscustomobject]$hash

}

 

The end result is a collection of objects that I can use with other PowerShell commands.

​Computername   : CLIENT8

OS             : Microsoft Windows 8 Pro

OSArchitecture : 64-bit

ProcessCount   : 88

PSVersion      : 3.0

Computername   : COREDC01

OS             : Microsoft Windows Server 2008 R2 Standard

OSArchitecture : 64-bit

ProcessCount   : 39

PSVersion      : 2.0

Computername   : QUARK

OS             : Microsoft Windows 8 Release Preview

OSArchitecture : 32-bit

ProcessCount   : 41

PSVersion      : 3.0

 

It’s important to remember, especially with regards to PowerShell 3.0, is to make sure you know how to use hash tables. I suspect they will play an even bigger role in your PowerShell work.

Once again, as we’ve seen here and throughout this series, PowerShell is all about the objects. There are a variety of ways to create and modify them. You’ll also most likely see examples of all these techniques in scripts you find on the Internet. But now, I trust you are eager to put your new found knowledge to work. If you run into issues or have questions, feel free to post in the Ask Don and Jeff forum at PowerShell.com.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

Don't leave your business open to attack! Come learn how to protect your AD in this FREE masterclass!REGISTER NOW - Thursday, December 2, 2021 @ 1 pm ET

Active Directory (AD) is leveraged by over 90% of enterprises worldwide as the authentication and authorization hub of their IT infrastructure—but its inherent complexity leaves it prone to misconfigurations that can allow attackers to slip into your network and wreak havoc. 

Join this session with Microsoft MVP and MCT Sander Berkouwer, who will explore:

  • Whether you should upgrade your domain controllers to Windows Server
    2019 and beyond
  • Achieving mission impossible: updating DCs within 48 hours
  • How to disable legacy protocols and outdated compatibility options in
    Active Directory

Sponsored by: