Am getting blanks for this line of code, why?

Welcome Forums General PowerShell Q&A Am getting blanks for this line of code, why?

This topic contains 8 replies, has 4 voices, and was last updated by

 
Participant
4 weeks ago.

  • Author
    Posts
  • #146003

    Participant
    Points: 476
    Rank: Contributor

    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.

    Thanks.

  • #146012

    Participant
    Points: 1,373
    Helping Hand
    Rank: Community Hero

    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) {
        [PSCustomObject]@{
            Number = $Number
            Remainder = $Number % 2
        }
    }
    • #146033

      Participant
      Points: 476
      Rank: Contributor

      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."

  • #146016

    Participant
    Points: 476
    Rank: Contributor

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

  • #146036

    Participant
    Points: 1,373
    Helping Hand
    Rank: Community Hero

    When I tried to pipe the Foreach to Export-csv, ...

    $Result = foreach ($Number in $d) {
        [PSCustomObject]@{
            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.

    • #146040

      Participant
      Points: 476
      Rank: Contributor

      Many thanks!

  • #146123
    js

    Participant
    Points: 962
    Helping Hand
    Rank: Major Contributor

    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
    
  • #146228

    Participant
    Points: 659
    Helping Hand
    Rank: Major Contributor

    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. 🙂

    • #146237

      Participant
      Points: 476
      Rank: Contributor

      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.

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort