Am getting blanks for this line of code, why?

I have the ff:

$d = @(1,2,3,4,5,6,7)

$d | Select-object *,@{N='Remainder'; E=($_%2).ToString()}

and I get blanks for the Remainder. Would be grateful for any tips, advice.


Simple numbers do not have properties you could select with Select-Object. You would need a Foreach-Object or a foreach.

foreach ($Number in $d) {
Number = $Number
Remainder = $Number % 2

Sorry, just saw Olaf’s reply, much appreciated and many thanks.

Thanks again Olaf. When I tried to pipe the Foreach to Export-csv, it gave me an error before I could even run it, saying: “An empty pipe element is not allowed.”

[quote quote=146033] When I tried to pipe the Foreach to Export-csv, …[/quote]

$Result = foreach ($Number in $d) {
Number = $Number
Remainder = $Number % 2
$Result |
Export-Csv -Path .\Remainders.csv -Delimiter ‘,’ -NoTypeInformation

You collect the output in a variable and pipe this to Export-Csv.

Many thanks!

You can generate what you need with select-object.

1..7 | select @{n='Number'; e={$_}}, @{n='Remainder'; e={$_ % 2}}

Number Remainder
------ ---------
     1         1
     2         0
     3         1
     4         0
     5         1
     6         0
     7         1

Yep, js has the right syntax. Your original, Ramon, was missing the curly braces in your Expression value; it takes a script block value (for custom/calculated properties) or a string (for renaming an existing property) only. :slight_smile:

Much thanks, Olaf, JS and Joel – I made that mistake before!

I cannot thank you enough for all of your comments and tips.

They provide many insights to the fundamentals of using Powershell and reinforce my learning progress.

More power to you all.