3

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
wecandoit
  • 37
  • 6

2 Answers2

1

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
Jawad
  • 11,028
  • 3
  • 24
  • 37
  • Unfortunately it doesn't work ad you said, I'll post a screenshot and I think the second time you want to add a property you forgot to write `-NotePropertyName` and `-NotePropertyValue`. You can see here that it doesn't work as you said @Jawad https://imgur.com/a/psnAVX3 I think the problem is because I'm printing it before modifing it.. how can I print it before modifing it and then print it again to see the changes? – wecandoit Feb 24 '20 at 23:35
  • @wecandoit I tested it before posting and found it functional. You can simply write the name of the object in one line and it will print it. [See example here](https://tio.run/##dY29CsIwGEX3PEUorn0EQamCg9aA4CIOaXNpIwkp6ZfYoj57/BvUwfEeLud07gzftzAmpYkdt9UJNfEpP4hdEXpy9k2OswsrnO0CwZfS4vHIZFVnbCFJPscwDBm7MfZxXPlcqXwDW8HzvHQE4V0HT@NLsEaE@eV7aQL4SjftP43wOmqDBnxpECVBfRVTugM) – Jawad Feb 24 '20 at 23:44
  • Hi thanks for your reply, if you check the photo I posted in the prior comment you will see that the modifications are not printed I think because I have also printed the object before the modifications, can you please take a look? How can I print the object before the modifications and later to see the changes? Cause How I did in the photo does not work. Thanks in advance for your patiance. – wecandoit Feb 24 '20 at 23:48
  • @wecandoit what your screenshot doesnt show is if you are running the same script or different test.ps1 file. It's quite possible you have a different test.ps1 file you are running vs what you have open. Also make sure you save the changes and then run – Jawad Feb 24 '20 at 23:52
  • look here: https://tio.run/##dY29CsIwGEX3PEUorn0EQamCg9aA4CIOaXNpIwkp6ZfYoj57/BvUwfEeLud07gzftzAmpYkdt9UJNfEpP4hdEXpy9k2OswsrnO0CwZfS4vHIZFVnbCFJPscwDBm7MfZxXPlcqXwDW8HzvHQE4V0HT@NLsEaE@eV7aQL4SjftP43wOmqDBnxpECVBfRVTugM – wecandoit Feb 24 '20 at 23:54
  • As I tried to say the issue is because I'm trying to print the object also before modifying it, how can I print it before the changes and later to see the changes? – wecandoit Feb 24 '20 at 23:55
  • @wecandoit one way to go about it would be to use a hashtable and then convert it to pscustomobject once you are done. [See here](https://tio.run/##XY3BCgIhFEX37yvk0aIg@oOgqHZR7aOFo6/RUBR1zKi@3WbaDLU8957L9e5OISoyptaJfRybG4nElmz1hI2zvksUDtxSnyBvBMKWJz5AKQXhDePkxa4JRl6spZzinjIZnDNUulU4@69PQWdtqKVB2RnKPJHsNTj7KLqYnHVf@8J@f2r9AA) – Jawad Feb 25 '20 at 00:11
  • @wecandoit and a [reference to a post by @mklement](https://stackoverflow.com/a/45705068/1390548) – Jawad Feb 25 '20 at 01:40
0

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.

Steven
  • 6,817
  • 1
  • 14
  • 14
  • Hi @steven Thanks for the reply I really appreciate it, may I know why if I run `$myObject` before modifing it and then after modifing it I do not see the changes but it prints only the first created object? How can I print the created object `$myObject` before and after the changes? Thanks again in advance :) – wecandoit Feb 24 '20 at 23:38
  • You have to reassign the variable. I updated the answer a bit to reflect that. – Steven Feb 25 '20 at 01:06