Fast Approach to Creating New Objects

by Nov 9, 2016

To wrap up multiple pieces of information, you best store them in custom objects. The easiest and fastest way is to use the PSCustomObject:

#requires -Version 3.0
$o = [PSCustomObject]@{
    Date     = Get-Date
    BIOS     = Get-WmiObject -Class Win32_BIOS
    Computer = $env:COMPUTERNAME
    OS       = [Environment]::OSVersion
    Remark   = 'Some remark'
}

Inside the braces, you assign pieces of information (or command results) to keys. This produces an object combining all pieces of information into one:

 
PS C:\> $o


Date     : 10/28/2016 3:47:27 PM
BIOS     : \\DESKTOP-7AAMJLF\root\cimv2:Win32_BIOS.Name="1.4.4",SoftwareElementID="1.4.4",SoftwareElementState=3,TargetOpera
           tingSystem=0,Version="DELL   - 1072009"
Computer : DESKTOP-7AAMJLF
OS       : Microsoft Windows NT 10.0.14393.0
Remark   : Some remark




PS C:\> $o.Remark
Some remark

PS C:\> $o.OS 

Platform ServicePack Version      VersionString                    
-------- ----------- -------      -------------                    
 Win32NT             10.0.14393.0 Microsoft Windows NT 10.0.14393.0



PS C:\> $o.OS.VersionString
Microsoft Windows NT 10.0.14393.0

PS C:\> 
 

Twitter This Tip! ReTweet this Tip!