Best practice when creating a custom object with multiple values for each property?

> $LocalUsers = Get-CimInstance -ClassName Win32_UserAccount -ComputerName $ComputerName
> 
>     $UserObject = foreach ($User in $LocalUsers) {
>         [pscustomobject]@{
>             Name    = $User.Name 
>             Caption = $User.Caption
>             SID     = $User.SID
>             Domain  = $User.Domain
>         }
>     }
> 
>     $UserObject

For Example: I create a custom object, and then I set each property to the individual items value

Is this best practice? When creating a custom object, is this the best way to output an object like a table format, what about New-Object, is that also an acceptable way of outputting an object? Could you substitute this using New-Object

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

As I already told you in your last thread it is the best way wo combine properties from more than one object or array into one output. If you have only one source you can simply use a Select-Object. :man_shrugging:t3:

$LocalUsers = 
    Get-CimInstance -ClassName Win32_UserAccount -ComputerName $ComputerName

$LocalUsers | 
    Select-Object -Property Name, Caption, SID, Domain

And BTW: It does not make that much sense to create a new variable when your original variable $LocalUsers already has all the properties. :wink:

Oh I see so you only would need to do the custom object when combining multiple properties?

Is the variable → $UserObject what you mean by

does not make that much sense to create a new variable when your original variable $LocalUsers already has all the properties

Again … you can combine properties from different sources. :man_shrugging:t3:

Yes. All the properties you have in $UserObject you already have in $LocalUsers. :man_shrugging:t3:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.