Author Posts

January 1, 2012 at 12:00 am

by sandheepkrishna at 2012-11-05 13:07:26


Get-WmiObject -ComputerName mypc -Class Win32_Service -Filter "state='running'" | select caption

above command will displays the all services that are running on the computer mypc, my question is how can i export this in csv file with first column is computername and second column is all services, (i dont need more columns).

or how can i multi line output of above command to single line as exportable to csv.

by coderaven at 2012-11-05 19:51:29

I would approach it with a PSObject

$Systems = Get-Content Computers.txt
$Data = @()
$Systems | foreach-object {$Data += (New-Object PSObject -Property @{computer=$_; "Running Services"=(Get-WMIObject -Computer $_ -Query "Select caption, state from Win32_Service where State=`"Running`"" | Select-Object -ExpandProperty Caption)})}

Now $Data contains your information. Use Export-CSV or Export-CLIXML.

by RichardSiddaway at 2012-11-07 10:45:22

First point is that your csv will be very difficult to use if you only have two columns. I would strongly recommend that you have one line per running service that contains the computer name and service name. You don't state if you want the service name of the display name.

The WMI call looks like this
Get-WmiObject -Class Win32_Service -Filter "State = 'Running'" -Property SystemName, Name, DisplayName -ComputerName $env:COMPUTERNAME |
select SystemName, Name, DisplayName

If you can use PowerShell v3 try the Get-CimInstance cmdlet

Get-CimInstance -ClassName Win32_Service -Filter "State = 'Running'" -Property SystemName, Name, DisplayName |
select SystemName, Name, DisplayName

Use the property parameter in both cases to restrict the data being returned from the remote machine – it will reduce your net work traffic