locked
ForEach Script Block RRS feed

  • Question

  • I am in the process of learning PowerShell and have been reading about the ForEach statement.  The simple code below is based on a script that I found online, and I have been trying to modify it to perform two separate operations.  I want to stress this point.  I am not looking for a clever way to consolidate the math into one operation.  I am purposely doing two operations in order to learn how to perform multiple operations in a script block.  Seems like it should be easy, but I can't seem to make it work so there is obviously something that I am missing.

    Here's the script:

    $NumArray = (1..10)
    ForEach ($Item in $NumArray)
    {
        $Item * 13
        $Item + 20
        $Item
    }

    In my mind, it should be doing 1 * 13 = 13 + 20 = 33, then 2 * 13  = 26 + 20 = 46, and so forth.  The output I am trying to get is:

       33

       46...

    What I am getting is:

       13

       21

       1

       26

       22

       2...

    Okay, I understand why I am getting that output.  I just can't figure out how to get the output that I want.  What am I doing wrong?

    Thanks in advance for any help that you can offer!

    --Tom

    Wednesday, April 10, 2019 4:05 PM

Answers

  • You are not updating the value of $item, you are just displaying the value of $item times/plus some number.

        $Item = $Item * 13          
        $Item = $Item + 20
        $Item

    Wednesday, April 10, 2019 4:22 PM

All replies

  • You are not updating the value of $item, you are just displaying the value of $item times/plus some number.

        $Item = $Item * 13          
        $Item = $Item + 20
        $Item

    Wednesday, April 10, 2019 4:22 PM
  • It is still you math that is wrong.  You need to actually learn PowerShell and not guess.

    foreach($item in (1..10)) {
        $item *= 13
        $item += 20
        $item
    }

    This version  of "foreach" is not capitalized.  "ForEach-Object" is a CmdLet and IS capitalized.



    \_(ツ)_/

    Wednesday, April 10, 2019 4:39 PM
  • It is still you math that is wrong.  You need to actually learn PowerShell and not guess.

    foreach($item in (1..10)) {
        $item *= 13
        $item += 20
        $item
    }

    This version  of "foreach" is not capitalized.  "ForEach-Object" is a CmdLet and IS capitalized.



    \_(ツ)_/

    Did I not say, in the FIRST sentence of my post, that I am in the process of *learning* PowerShell?  Was that somehow unclear to you?
    Wednesday, April 10, 2019 7:46 PM
  • Did I not say, in the FIRST sentence of my post, that I am in the process of *learning* PowerShell?  Was that somehow unclear to you?

    That disclaimer does not allow you to not learn PowerShell or how to use help. It is what everyone says here when they want someone else to do the learning for them.

    Start by either taking a tutorial or getting a book.  You cannot learn PowerShell by guessing and you cannot learn PowerShell without a fairly deep technical knowledge of Windows and computing.  A tech, by definition, knows how to learn a new technology before asking questions.  A tech always reads the full manual first.

    If you could learn by guesswork you would have seen the issue with your code immediately.

    In math we have to assign a result to a calculation if we want to use it in future steps.  That is actually not even math.  It is elementary school arithmetic.

    If you take basic training all of this will become clear.

          Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Wednesday, April 10, 2019 9:06 PM
  • Hi,

    Thanks for your question.

    I think you need to learn more about PowerShell variables.

    https://www.howtogeek.com/141099/geek-school-learning-powershell-variables-input-and-output/

    If you has solved your issue by other's reply, please remember to mark as answer to help other community members find the helpful reply quickly.

    Best regards,

    Lee


    Just do it.

    Thursday, April 11, 2019 2:56 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Just do it.

    Thursday, April 25, 2019 9:07 AM
  • Sorry for the delay in responding.  I was turned off by a previous reply and did not bother returning to this thread until I came across it again the other day.

    Yes, I resolved the issue.  When I came back to it, I saw the problem instantly.  I modified the script as follows:

    cls
     
    Write-Host "Classic ForEach Loop Output" -ForegroundColor Green
    
    Write-Host
    
    $NumArray = (1..10)
    
    ForEach ($Item in $NumArray) {
        $Item = $Item * 13
        $Item = $Item + 20
        $Item
    }

    I have marked the reply by MotoX80 as the solution since he proposed the same change.

    --Tom

    Monday, December 2, 2019 2:35 PM