Create custom object

Hi,

I’m trying to create a custom object that has values from three different variables.
Imagine this scenario:

Variable1 content:
User1
User2
User3

Variable2 content:
User5
User7
User8

Variable3 content:
User10
User12
User20

I want to create an object that is something like this:
“Local Admins”;“Remote Desktop”;“Logons”
User1;User5;User10
User2;User7;User12
User3;User8;User20

Is there a way?

Of course.

$props = @{ColumnA = $var.Value1
           ColumnB = $var.Value2}
New-Object -Type PSObject -Prop $props

That’s the basic format. Do that for each row you want to produce. If you need the final result to be delimited with semicolons, as in your example, collect those rows into a variable and:

$AllRows | ConvertTo-CSV -Delim “;”

And you’ll get a semicolon-delimited set.

Don Jones,

Wow, you’re fast!
But how can I do it when I don’t know how many values are in those variables?
For example: Variable1 can have 3 values, Variable2 can have 10 values and Variable3 can have 6 values…

Mmm, that’s not really how objects work. Objects don’t have variable numbers of properties. If your end goal is that example you provided, then that’s not a good format for variable data. Meaning, if one machine can have multiple “Local Admins,” PowerShell can represent that in an object, but it becomes hierarchical data. You can’t represent hierarchical data in a flat file.

Think of an Excel sheet. If you were to do this in an Excel sheet, how would you put multiple values into a single cell for “Local Admins” on one machine? Maybe you’re thinking, “oh, I’d just do like commas between them,” and that might be fine for you as a human, but it’s a sucky data format for computers to deal with. That’s why computers have relational databases instead of doing everything in CSV files or Excel sheets.

So what’s the end goal, here? What are you trying to get to?

Ditto to what DonJ stated here.

Yet, you’d also might want to wrap you head around PoSH multi-dimensional arrays.

See these discussions.

Today I want to talk a little about multidimensional arrays in Windows Powershell. Multidimensional arrays are one of the complex data types supported by Powershell. They can be used to dynamically store information in a volatile table without having it written to a real database in a file on the disk.

Streaming Tips For Beginners - StreamTipz

Easily Create and Manipulate an Array of Arrays in PowerShell 'blogs.technet.microsoft.com/heyscriptingguy/2011/12/09/easily-create-and-manipulate-an-array-of-arrays-in-powershell'
Comparing two multi-dimensional arrays 'powershell.org/forums/topic/comparing-two-multi-dimensional-arrays'

Don Jones and postanote,

I need to create a report of users that are in the local Admins, Remote Desktop users and who logged on multiple servers.
I was able to create a TXT with that information, but I was trying to create an HTML with that info side by side.
Microsoft auditing reasons.
But that’s ok, I think I’ll try to use an Excel file to output the data…
I’ll take a look at those sites postanote!

Thanks for your time and attention!!!

Hey Don, anything wrong with making objects this way?

[pscustomobject]@{ColumnA = $var.Value1
                  ColumnB = $var.Value2}