wecandoit
wecandoit

Reputation: 37

Creation and modification of Objects in Powershell

I'm a newbie of powershell, I'm starting right now to look at objects, etc. I'm creating an object in this way:

$myObject = [PSCustomObject]@{
  ComputerName = "abc"
  Data = "xxx"
}

If I then print $myObject what i get is:

   ComputerName Data
   ------------ ----
   abc          xxx

And everything is ok, now I want to add a property to that object, and I saw (tell me if i'm wrong) that I can do it in 2 ways: with add-member and with select-object For example with add-member what I did was:

$myObject | Add-member -NotePropertyName Level -NotePropertyValue Highest

Instead with Select-object I did:

$myobject = 2 (cause i want to add 2 properties, is it right?) | Select-Object -Property Level, Privilege
$myobject.Level = "High"
$myobject.Privilege = "Elevated"

Now if I run $myobject I still only get:

   ComputerName Data
   ------------ ----
   abc          xxx
  1. What should I do to see all the data, even the one that I added later?
  2. Can I directly add the values to the properties added through Select-Object? Thanks

Upvotes: 3

Views: 317

Answers (2)

Steven
Steven

Reputation: 7057

You can use either Add-Member or Select-Object. Withstanding the advantages or disadvantages in different situations I just want to throw in the Select-Object example. Just so you have both methods:

This example will echo the object with the selected properties:

$myObject | 
Select-Object *,
    @{Name = 'Level'; Expression = { 'High' } },
    @{Name = 'Privilege'; Expression = { 'Elevated' } }

If you want to save the new properties back to the object variable you'll have to reassign like below:

$myObject = $myObject | 
Select-Object *,
    @{Name = 'Level'; Expression = { 'High' } },
    @{Name = 'Privilege'; Expression = { 'Elevated' } }

PowerShell allows you to provide a hash table to define the new properties. You'll note it looks fairly similar to the hash you used to create the object. Typically the expression would leverage the $_ syntax to calculate the property's value. You will often here these referred to as calculated properties.

Upvotes: 0

Jawad
Jawad

Reputation: 11364

You can use the Add-Member method on a PsCustomObject.

$myObject = [PSCustomObject]@{
ComputerName = "abc"
Data = "xxx"
}

$myObject | Add-Member -NotePropertyName Level -NotePropertyValue High
$myObject | Add-Member Privilege Elevated

$myObject

#Output looks like,

ComputerName Data Level Privilege
------------ ---- ----- ---------
abc          xxx  High  Elevated 

Update

Not sure at the moment why but will elaborate on it ...

If you print the Pscustomobject and then run the add-member, they seem to be ignored. If you create a hashtable, update it and then convert to PsObject, it works. Following is an example of that hashtable

$myObject = @{
ComputerName = "abc"
Data = "xxx"
}
$myObject | ft

$myObject.Add("Level", "high")
$myObject.Add("Privilege", "Elevated")

[pscustomobject] $myObject | ft

What I found

When you print $myObject then add the data, the data is added but not displayed. This is due to some internal mechanism, unknown to me, that continues to use the same headers from previous command.

If you notice, the data is printed twice under the same heading. If you want to see the differences before and after, pipe it to format-list or format-table to use a different output stream each time.

$myObject = [PSCustomObject]@{
ComputerName = "abc"
Data = "xxx"
}
$myObject | ft

$myObject | Add-Member -NotePropertyName Level -NotePropertyValue High -Force
$myObject | Add-Member Privilege Elevated

$myObject | ft

Upvotes: 1

Related Questions