Process all objects in Foreach

Welcome Forums General PowerShell Q&A Process all objects in Foreach

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

 
Participant
1 year, 6 months ago.

  • Author
    Posts
  • #69603

    Participant
    Points: 0
    Rank: Member

    Hi,

    Is it possible to process all objects in a foreach loop without waiting for the previous object to complete?

    So for instance

    $Services = Get-Service
    Foreach ($Service in $Services)
              {
                 Stop-Service -Name $Service.
              }
    

    Is there a way to do all in one hit?

    Cheers

  • #69604

    Participant
    Points: 21
    Rank: Member

    You could do it with a workflow

    workflow MyStopService
    {
         $services = get-service
         foreach -parallel ($service in $services)
         {
              stop-service -name $service
         }
    }
    

    Now this doesn't do anything about services that stop one service when stopping themselves – but this is at least one way you could accomplish what you were looking to do.

  • #69624

    Participant
    Points: 0
    Rank: Member

    Thanks Paul, the workflow seems to have done the trick.

    Cheers

The topic ‘Process all objects in Foreach’ is closed to new replies.