0

I have a question: I want to store data in a variable with three columns and then process it. So I looked at an example with hash tables, which seemed great but then I need three columns, and I want to be able to run queries against this with it having say 100 rows.

What's the best way of doing this?

Example

2 Answers 2

3

You can create custom objects, each with three properties. That will give you the three column output. If you have V3 you can create custom objects using a hashtable like so:

$obj = [pscustomobject]@{Name='John';Age=42;Hobby='Music'}
PS> $obj | ft -auto

Name Age Hobby
---- --- -----
John  42 Music

If you are on V2 you can create these objects with New-Object:

$obj = new-object psobject -Property @{Name='John';Age=42;Hobby='Music'}
Sign up to request clarification or add additional context in comments.

2 Comments

If you have v2 or v1, you can still create an object to do it, it just requires a little more work. You'll need to look at New-PSObject and Add-Member to replace that first line.
@HunterEidson Sure. But are folks really still hanging onto V2? Folks, move to v3 at the very least. ;-) That said, I updated my answer to show how to do this in V2.
2

I would create an array or collection of custom PS Objects, each having 3 properties, then use the Powershell comparison operators on that array/collection to do my queries.

see:

Get-Help about_object_creation
Get-Help about_comparison_operators
Get-Help Where-Object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.