Getting computer name in the out put

I run the command:
PS C:\temp> $computerlist = Get-Content .\complist.txt
PS C:\temp> Get-WmiObject win32_service -comp $computerlist | Where-Object { $ -eq "bits"} | ft

I get this:
           ExitCode Name                          ProcessId StartMode           State               Status
           -------- ----                          --------- ---------           -----               ------
               1077 BITS                                  0 Manual              Stopped             OK
               1077 BITS                                  0 Manual              Stopped             OK
               1077 BITS                                  0 Manual              Stopped             OK

Well, how do I know which one's for which server in the server list I provided in the variable? is there a way tp get the computer name against each of those entries no matter what I chose to, list or table?

What I am expecting:
Name     ExitCode Name       ProcessId      StartMode           State          Status
--------       --------      ----             ---------            ---------                -----               ------
 Server1   1077    BITS           0               Manual              Stopped             OK
 Server2   1077    BITS           0               Manual              Stopped             OK
 Server3   1077    BITS           0               Manual              Stopped             OK

I hope this makes sense.
LVL 10
Who is Participating?
For example like this (and replace the properties for "select" with the properties you want in the required order):
Get-WmiObject win32_service -ComputerName $ComputerList -Filter "Name='Bits'" | Select Systemname,Status,State,StartMode | ft

Open in new window

Something along the lines of this:  I normally use xml as my source to iterate over, but you can do something like the below.  Pipe the computer list into variable.  perform a for-each on the items.

$computerlist = Get-Content .\complist.txt
For each ($computer in $computerlist)
     Write-Host $computer
     Get-WmiObject win32_service -comp $computer | Where-Object { $ -eq "bits"} | ft
One other method I'll mention.  If you were using PSRemoting, each returned object would include the name of the computer it was returned from.
PS C:\test\PS> invoke-command -computername $computerlist {Get-WmiObject win32_service -Filter "Name='Bits'"} | ft

         ExitCode Name                    ProcessId StartMode        State            Status           PSComputerName
         -------- ----                    --------- ---------        -----            ------           --------------
                0 BITS                          912 Manual           Running          OK               wkstn1

Open in new window

ashwin_kumar525Author Commented:
Thanks for the response folks, each of those looks promising. let me try that tomorrow and let you know how it goes.
Add the computer name to the object you output.
$computerlist = Get-Content .\complist.txt
foreach ($computer in $computerlist)
   Get-WmiObject win32_service -comp $computerlist | Where-Object { $ -eq "bits"} | Select @{Name="Computer";Expression={$computer}}, ProcessId, Startmode, State, Status #any other properties you want to return

Open in new window

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.

All Courses

From novice to tech pro — start learning today.