7

Is it possible to create a Custom Object (PSObject) and define its properties beforehand and later in the program execution, we keep adding array of values to the object.

For e.g;

$c = @()

$c = New-Object PSObject
$c | Add-Member -type NoteProperty -name Name 
$c | Add-Member -type NoteProperty -name Gender
$c | Add-Member -type NoteProperty -name Age


$c | Add-Member -type NoteProperty -name Name -value "John"
$c | Add-Member -type NoteProperty -name Gender -value "Male"
$c | Add-Member -type NoteProperty -name Age -value "30"

Thanks in advance for any leads or advice.

Rajiv
  • 675
  • 9
  • 21
  • 35
  • 1
    I'm not sure why you set $c to an empty array before setting it to a new PSObject. The array was lost with the second assignment. – Mike Shepard Mar 13 '13 at 15:12
  • I have a script which does a list of checks for a set of computers. Hence I would like to record the status of each check for every computer by creating an object and placing the status of every computer in an array. The first check is to test-connection for the computer. I wanted to finish all the check for the computer and then record the status. – Rajiv Mar 14 '13 at 03:36
  • 1
    Fun fact: It's allowed to mark an answer as correct so questions get closed. :-) – Frode F. Mar 25 '16 at 17:09

2 Answers2

17

I'm not sure I follow. Do you want an array of objects with your specified properties? Because your sample first creates an array, that you then overwrite into a single object. So you lost your array.

You can create the object using new-object and specify the properties with values as a hashtable in the -Property parameter. Like this:

$c = New-Object psobject -Property @{
    Name = "John"
    Gender = "Male"
    Age = 30
}

To make an array of them, you can use:

$myarray = @()

$myarray += New-Object psobject -Property @{
    Name = "John"
    Gender = "Male"
    Age = 30
}

If you have multiple tests that you run one by one, you can run the tests in a function that tests and creates a "resultobject", then you collect it:

$myresults = @()

function mytests($computer) {
    #Test connection
    $online = Test-Connection $computer

    #Get buildnumber
    $build = (Get-WmiObject win32_operatingsystem -ComputerName $computer).buildnumber

    #other tests

    #output results
    New-Object psobject -Property @{
        Online = $online
        WinBuild = $build
    }
}

$myresults += mytests -computer "mycomputername"
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • is it the convention to specify the property value for the first array element? I mean, Can we not declare the array structure (with column names) and then add the array element in due course. Is it mandatory to set array element along with PSObject Initialization? – Rajiv Mar 14 '13 at 03:38
  • 1
    Do you mean if you have to add it to the array when you create it? Because you don't. You could use `$myobjname = new-object .....` and then add it later `$myarray += $obj` . In most cases you know the values for you object when you need to create it, so it's easier to just do it all together – Frode F. Mar 14 '13 at 07:26
  • @Rajiv: Arrays are not spreadsheets. Arrays contain objects; objects have properties with names and values. – Tim Sparkles Aug 21 '13 at 22:42
2

Yeah, so I know this is an old post but Don Jones did something like this:

$props = @{
    Name = "John"
    Gender = "Male"
    Age = 30
}

$c = New-Object PSObject -Property $props

You can run the following to see the Properties and Values the new Object:

c$ | Get-Member

I think that's what you're looking for.

user229044
  • 232,980
  • 40
  • 330
  • 338
mcX
  • 21
  • 1