locked
Foreach command RRS feed

  • Question

  • Hi,

    I don't understand when to use foreach command. For example if I need to install specific role on many machines and if I need to run many command all the time on those servers then I can create for example

    $servers = 'server1','server2'

    Then I can create a new pssession

    $sessions = New-PSSession -ComputerName $servers

    icm -session $s {Install-WindowsFeature Hyper-V}

    I can do everything without foreach so why I need that command? Is that only when doing scripts? Please tell me without saying learn powershell.

    Friday, October 6, 2017 9:35 AM

Answers

  • Hi Deni,

    Not all cmdlets can  accept multiple inputs or an array of inputs like you have demonstrated above.

    Get-Help New-PSSession -Parameter ComputerName
    
    -ComputerName <String[]>

    Shows that the -ComputerName parameter accepts a String. The [] indicates that it can accept an array of strings or multiple inputs of strings.

    From this link:

    The Foreach statement (also known as a Foreach loop) is a language construct for stepping through (iterating) a series of values in a collection of items.

    The simplest and most typical type of collection to traverse is an array. Within a Foreach loop, it is common to run one or more commands against each item in an array.

    For instances where a cmdlet can't accept multiple inputs, you can use a foreach loop to iterate through the array.


    Thanks, Tim. | Please remember to mark the replies as answers if they help. |

    • Proposed as answer by jrv Friday, October 6, 2017 10:07 AM
    • Marked as answer by Deni Garo Friday, October 6, 2017 11:05 AM
    Friday, October 6, 2017 9:59 AM

All replies

  • Hi Deni,

    Not all cmdlets can  accept multiple inputs or an array of inputs like you have demonstrated above.

    Get-Help New-PSSession -Parameter ComputerName
    
    -ComputerName <String[]>

    Shows that the -ComputerName parameter accepts a String. The [] indicates that it can accept an array of strings or multiple inputs of strings.

    From this link:

    The Foreach statement (also known as a Foreach loop) is a language construct for stepping through (iterating) a series of values in a collection of items.

    The simplest and most typical type of collection to traverse is an array. Within a Foreach loop, it is common to run one or more commands against each item in an array.

    For instances where a cmdlet can't accept multiple inputs, you can use a foreach loop to iterate through the array.


    Thanks, Tim. | Please remember to mark the replies as answers if they help. |

    • Proposed as answer by jrv Friday, October 6, 2017 10:07 AM
    • Marked as answer by Deni Garo Friday, October 6, 2017 11:05 AM
    Friday, October 6, 2017 9:59 AM
  • A lot of people use foreach anyway when they don't need to, but Don Jones will get upset  http://windowsitpro.com/blog/foreach-makes-me-die-little-inside.  It's up to you.  You could also use it whenever you want a script block.  But very frequently you can pipe a comma seperated list to a command, because powershell functions easily support it with their process blocks. 

    write-output *.bat,*.ps1 | get-childitem

    Being able to use a list with a command argument is more rare.  They would have to put in an extra foreach.

    get-process -name chrome,dwm







    • Edited by JS2010 Friday, October 6, 2017 6:04 PM
    Friday, October 6, 2017 2:13 PM