• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 994
  • Last Modified:

powershell foreach-obj and get-services

Hi, how do i fix either one of these commands?  i get an error when i try to run it


get-service | foreach-object { if ($_.status -eq "stopped") {start-service $_}}

2nd variation

$service = get-service
get-service | foreach-object { if ($_.status -eq "stopped") {start-service $service}}
0
Kylo Ren
Asked:
Kylo Ren
1 Solution
 
KenMcFCommented:
Try this

get-service | foreach-object { if ($_.status -eq "stopped") {start-service $_.Name}}

Or

$service = get-service
$service | foreach-object { if ($_.status -eq "stopped") {start-service $_.name}}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now