Skip to content

Objects

VectorBCO edited this page Jun 29, 2021 · 6 revisions

Goto topics list

Objects in PowerShell

Definition

Wiki: Object is an entity in the digital space with some characteristics and behavior, some properties (attributes) and their specific operations (methods).

Creation methonds

Acquiring object or a list of objects as a result of implementing cmdlet\function\script

# $Service will contain the first object (service by name)
$Service = Get-Service | Select-Object -First 1 

# $Services will contain object list (objects in a list will be identical structure to previous example)
$Services = Get-Service | Where-Object {$_.Status -eq 'Running'}

Creating an object copy and its modification

$Service = Get-Service | Select-Object Name, ServiceName, StartupType, BinaryPathName -First 1
$Service_Copy = $Service | Select-Object Name, ServiceName, @{N="StartupType";E={"JustMagic"}}, BinaryPathName

# Comparison of 2 objects saved in $Service and $Service_Copy shows that they differ in one parameter but match in 3 others.
Compare-Object -ReferenceObject $Service -DifferenceObject $Service_Copy -Property Name, ServiceName, StartupType, BinaryPathName

Creating an object

# New-Object\Add-Member - Old school method (multiple options to configure parameters)
$Object_1 = New-Object -TypeName PSObject
$Object_1 | Add-Member -MemberType NoteProperty -Name Name -Value "TestObject" -Force
$Object_1 | Add-Member -MemberType AliasProperty -Name RefName -Value Name -Force
$Object_1 | Add-Member -MemberType NoteProperty -Name Description -Value "Some Description" -Force
$Object_1 | Add-Member -MemberType NoteProperty -Name StartupType -Value "JustMagic" -Force

# Hash table - quick and easy
$Object_2 = [PsCustomObject]@{ Name = "TestObject"; RefName = "TestObject"; Description = "Some Other Description"; StartupType = "JustMagic" }

# Comparison of 2 objects saved in $Service and $Service_Copy shows that they differ in one property but match in 3 others
Compare-Object -ReferenceObject $Object_1 -DifferenceObject $Object_2 -Property Name, RefName, Description, StartupType

# Object creation with Select-Object
$Bike = "" | Select-Object @{N="Name"; E={"Bike"}}, @{N="Producent"; E={"Yamaha"}}, @{N="Model"; E={"YZF-R1"}}, @{N="Color"; E={"Krasnenkiy"}}

Viewing object properties and methods

# Get-Member shows the properties of all *unique* objects if there is more than one
$Services = Get-Service
$Services.Count
# Depends on using Get-Member cmdlet may show properties and methods for each unique object
$Services | Get-Member
# Or properties for exact object (may be different if we are talking about object array)
Get-Member -InputObject $Services

# If the object types are mixed (more than one), Get-Member will show 2 or more sets of parameters and methods (if input provided through pipe)
$DifferentObjArray = @()
$DifferentObjArray += $Services
$DifferentObjArray += (Get-Process)
$DifferentObjArray.Count
$DifferentObjArray | Get-Member

# If property name is unknown, use Format-List * and check name by its value
# Select 1 object from the list and check 
$Services | Select -First 1 | Format-List *
# Same trick can be done with Select-Object *

Checking object type

In addition to Get-Member, the object type can be checked using GetType() method. It is important to understand that GetType(), unlike Get-Member, will show the direct array checking in addition to object class in the array

# Types will differ
($Services) | Get-Member
($Services).GetType()

# Types will match
($Services | Select -First 1) | Get-Member
($Services | Select -First 1).GetType()