1
$obj = New-Object -Type PSObject
$obj | Add-Member -Type ScriptProperty -Name ArrayMember {"" -split ","}
$obj.ArrayMember.GetType().Name

Output: String

$obj = New-Object -Type PSObject
$obj | Add-Member -Type ScriptProperty -Name ArrayMember {"1" -split ","}
$obj.ArrayMember.TetType().Name

Output: String

$obj = New-Object -Type PSObject
$obj | Add-Member -Type ScriptProperty -Name ArrayMember  {"1,2" -split ","}
$obj.ArrayMember.GetType().Name

Output: Object[]

I want to directly use $obj.ArrayMember += 3 to append 3 into the array, but if it return a string, like "1", the result will be 13, but I expected [1,3].

3
  • 1
    similar issue: stackoverflow.com/questions/18476634/… Commented Aug 25, 2019 at 3:21
  • 2
    ,("" -split ","), ,("1" -split ","), ,("1,2" -split ","). Note that it first case you have single element array, but not empty array. Commented Aug 25, 2019 at 7:30
  • 1
    declare your object property as an array first. that way the property is defined as an array. something like this ... >>> $TestObject = [PSCustomObject]@{ArrayPropOne = [array]@()} <<< however, you may want to define it as a generic.list since that has a working .Add() method. [grin] Commented Aug 25, 2019 at 11:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.