Display arrays from object

Hello,

I created object that have 2 simple arrays.

[pre]$obiekt = New-Object psobject -Property @{
‘Name’ = @(‘Name1 Surname1’, ‘Name2 Surname2’);
‘UserPrincipalName’ = @(‘name1.surname1@XXX’, ‘name2.surname2@YYY’);
};[/pre]

When I display this object by call its name $obiekt I got:

[pre]UserPrincipalName Name


{name1.surname1@XXX, name2.surname2@YYY} {Name1 Surname1, Name2 Surname2}[/pre]

How to display values in columns?

I tried:

[pre]$obiekt | Format-Table[/pre]

and many other solutions, but without success.

$obiekt = @() # Explicitly declares empty array

$obiekt += New-Object psobject -Property @{
    Name              = 'Surname1'
    UserPrincipalName = 'name1.surname1@XXX'
}

$obiekt += New-Object psobject -Property @{
    Name              = 'Surname2'
    UserPrincipalName = 'name2.surname1@XXX'
}

$obiekt

Yeah, you can’t have a single property with multiple values and have it display in columns. PS’s display framework is designed around arrays of objects, rather than arrays of individual items. Gotta split that object in two to get the column display you’re after. :slight_smile:

Okay. Now I understand. Thank you :slight_smile:

You cannot do this with splitting the strings. Roughly, for example…

 

$obiekt = @()

'Name1 Surname1', 'Name2 Surname2' | 
ForEach {
    $obiekt += New-Object psobject -Property @{
        Name              = ($PSItem -split ' ')[1]
        UserPrincipalName = $(($PSItem -split ' ')[0] + '.' + ($PSItem -split ' ')[1]) + '@XXX'
    }
}

$obiekt

# Results
<#
UserPrincipalName  Name    
-----------------  ----    
Name1.Surname1@XXX Surname1
Name2.Surname2@XXX Surname2
#>

Now, understand string concats can be a bit cumbersome. Yet, there are many ways to do it. The above is just Q&D (quick and dirty).