1

This is probably a dumb question but I cant seem to figure it out. How do I add a header to already existing variable? I have a variable with bunch of strings in it and I am trying to make it so it has a header which will simplify the script later on. Just as an example

$test = 1,2,3,4,5,6

Which comes out to be:

PS C:\windows\system32> $test
1
2
3
4
5
6

Where as what I want it to do is:

PS C:\windows\system32> $test
Numbers
--------
1
2
3
4
5
6

Additionally when implementing for each loop is it possible to add a blank header like to existing variable (from which foreach loop is running) and fill it automatically? for example going from original variable:

Letters   Value 
-------   -----  
a         10
b         15
c         23
d         25

To after for each loop:

Letters   Value   Numbers
-------   -----   ------
a         10        1
b         15        2
c         23        3
d         25        4

This is a super generic example but basically i have one object with headers and when using a function someone made I am trying to populate the table with output of that function, the issue is that its returning stuff with no header and just returns the output only so I cant even make a hash table.

Thanks in advance.

2 Answers 2

2

In your example, your variable is a list of integers.

That's why there's no header.

If your variable were something else, like, a custom object, it would be displayed with headers.

To make your example a list of custom objects:

$test = 1..6
$test | Foreach-Object { [PSCustomObject]@{Number=$_} }

You can save this back to a variable:

$test = 1..6
$testObjects = $test | Foreach-Object { [PSCustomObject]@{Number=$_} }

If an object has four or fewer properties, it will be displayed as a table.

So you could also, say, make an object with two properties and still get headers.

$test = 1..6
$test | Foreach-Object { [PSCustomObject]@{Number=$_;NumberTimesTwo = $_ * 2} }

If you want to control how any object displays in PowerShell, you'll want to learn about writing formatters. There's a module I make called EZOut that makes these a lot easier to work with.

Sign up to request clarification or add additional context in comments.

Comments

2

To offer an alternative to Start-Automating's helpful answer:

You can use Select-Object with calculated properties:

To turn a list of numbers into objects ([pscustomobject] instances) with a .Number property, whose display formatting defaults to the tabular display you're looking for:

$objects = 
  1,2,3,4,5,6 | Select-Object @{ Name='Number'; Expression={ $_ } }

Outputting $objects yields:

Number
------
     1
     2
     3
     4
     5
     6

You can use the same technique for adding additional properties to (copies of) existing objects, filling them at the same time (builds on the $objects variable filled above):

# Values for the new property.
$nums = 6..1 # same as: 6, 5, 4, 3, 2, 1
$i = @{ val = 0 } # Helper hashtable for indexing into $nums
$objects | Select-Object *, @{ Name='NewProperty'; Expression={ $nums[$i.val++] } }

Output:

Numbers NewProperty
------- -----------
      1           6
      2           5
      3           4
      4           3
      5           2
      6           1

If you want to add a property with $null values first and fill them later, use:

$objects | Select-Object *, NewProperty

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.