Search code examples
wmic

Filter microsoft service from `wmic service get` command


I'm using wmic to enumerate installed services:

wmic service get

Is it possible to hide microsoft service from it?

The query returns the following fields, none of them seems to be helpful:

AcceptPause AcceptStop Caption CheckPoint CreationClassName Description DesktopInteract DisplayName ErrorControl ExitCode InstallDate Name PathName ProcessId ServiceSpecificExitCode ServiceType Started StartMode StartName State Status SystemCreationClassName SystemName TagId WaitHint

Solution

  • This command can did the trick :

    WMIC service where "Not PathName like '%Micro%' AND Not PathName like '%Windows%'" get Name,DisplayName,PathName,State,Status
    

    And you can do it with Powershell script too :

    Refer to this answer here How to filter Microsoft Service using PowerShell?.

    You can filter the Non-Microsoft services with powershell script :

    $services = Get-WmiObject Win32_Service -Property Name,DisplayName,PathName | Select Name, DisplayName,PathName
    $serviceList = New-Object System.Collections.ArrayList
    foreach ($service in $services) {
      Try {
        $path = $service.Pathname.tostring().replace('"','')
        $cri = ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($path)).legalcopyright
        if ($cri -notlike "*Microsoft*") {
          $serviceList += $service
        }
      } catch {}
    }
    $serviceList
    

    Edit : on 02/11/2019 Vbscript Version

    Here is another version using vbscript in order to export the output results with Excel.

    Just copy and paste this code below as Non-Microsoft-Services.vbs and execute it by double click.

    Option Explicit
    ' Non-Microsoft-Services.vbs
    Dim objExcel,strComputer,objWMIService
    Dim State,colServices,x,objService,objWorksheet,objWorkbook
    ' Create a new and blank spreadsheet:
    Set objExcel = CreateObject("Excel.Application")
    Set objWorkBook = objExcel.WorkBooks.Add
    objExcel.Visible = True
    
    Set objWorksheet = objWorkbook.Worksheets(1)
    objWorksheet.Name = "Services Non-Microsoft"
    objWorksheet.Tab.ColorIndex = 3
    
    ' Format the cell A1 and add the text: Service Name
    objExcel.Cells(1, 1).Value = "Service Name"
    objExcel.Cells(1, 1).Font.Bold = TRUE
    objExcel.Cells(1, 1).Interior.ColorIndex = 43
    objExcel.Cells(1, 1).Font.ColorIndex = 2
    ' Format the cell A2 and add the text: Display Name
    objExcel.Cells(1, 2).Value = "Display Name"
    objExcel.Cells(1, 2).Font.Bold = TRUE
    objExcel.Cells(1, 2).Interior.ColorIndex = 43
    objExcel.Cells(1, 2).Font.ColorIndex = 2
    '*************************************************
    ' Format the cell A3 and add the text: State
    objExcel.Cells(1, 3).Value = "State"
    objExcel.Cells(1, 3).Font.Bold = TRUE
    objExcel.Cells(1, 3).Interior.ColorIndex = 43
    objExcel.Cells(1, 3).Font.ColorIndex = 2
    '*************************************************
    ' Format the cell A4 and add the text: Executable Path
    objExcel.Cells(1, 4).Value = "Executable Path"
    objExcel.Cells(1, 4).Font.Bold = TRUE
    objExcel.Cells(1, 4).Interior.ColorIndex = 43
    objExcel.Cells(1, 4).Font.ColorIndex = 2
    '*************************************************
    ' Format the cell A5 and add the text: Description
    objExcel.Cells(1, 5).Value = "Description"
    objExcel.Cells(1, 5).Font.Bold = TRUE
    objExcel.Cells(1, 5).Interior.ColorIndex = 43
    objExcel.Cells(1, 5).Font.ColorIndex = 2
    ' Find the Non-Microsoft Windows services on this computer
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colServices = objWMIService.ExecQuery("Select * From Win32_Service where Not PathName like '%Micro%' AND Not PathName like '%Windows%'")
    ' Write each service to Excel, starting in A2
    x = 1
    For Each objService in colServices
        x = x + 1
        objExcel.Cells(x, 1) = objService.Name
        objExcel.Cells(x, 2) = objService.DisplayName
        objExcel.Cells(x, 3) = objService.State
        objExcel.Cells(x, 4) = objService.PathName
        objExcel.Cells(x, 5) = objService.Description
        State = objService.Started
        If State Then 
            Cellule x,3,"Running"
            objExcel.Cells(x, 1).Font.ColorIndex = 10
            objExcel.Cells(x, 2).Font.ColorIndex = 10
            objExcel.Cells(x, 3).Font.ColorIndex = 10
            objExcel.Cells(x, 4).Font.ColorIndex = 10
            objExcel.Cells(x, 5).Font.ColorIndex = 10
        ELSE
            Cellule X,3,"Stopped"
            objExcel.Cells(x, 1).Font.ColorIndex = 3
            objExcel.Cells(x, 2).Font.ColorIndex = 3
            objExcel.Cells(x, 3).Font.ColorIndex = 3
            objExcel.Cells(x, 4).Font.ColorIndex = 3
            objExcel.Cells(x, 5).Font.ColorIndex = 3
        end if
    Next
    
    objExcel.Columns("A:A").EntireColumn.AutoFit
    objExcel.Columns("B:B").EntireColumn.AutoFit
    objExcel.Columns("C:C").EntireColumn.AutoFit
    objExcel.Columns("D:D").EntireColumn.AutoFit
    objExcel.Columns("E:E").EntireColumn.AutoFit
    
    Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim Network : Set Network = CreateObject("WScript.Network")
    Dim Computer : Computer = Network.ComputerName
    Dim xlVer,objXL
    Set objXL = CreateObject("Excel.Application") 
    ' Check Excel Version (12.0 = 2007)
    xlVer = Split(objXL.Version,".")(0) 
    If xlVer >= "12" Then
        objExcel.ActiveWorkbook.SaveAs fso.GetAbsolutePathName(".") & "\Non-Microsoft-Services_" & Computer & ".xlsx"
        objExcel.DisplayAlerts = True
    ' 56 = Excel 97-2003
    ' Voir la page http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat.aspx
    Else
        objExcel.ActiveWorkbook.SaveAs fso.GetAbsolutePathName(".") & "\Non-Microsoft-Services_" & Computer & ".xls",56
        objExcel.DisplayAlerts = True
    End If
    '--------------------------------------------------------------------
    Sub Cellule(X,NC,chaine)
        objExcel.Cells(X,NC).Value = Chaine
    End Sub
    '--------------------------------------------------------------------
    'Function to determine the current directory
    Function GetPath()
        Dim path
        path = WScript.ScriptFullName
        GetPath = Left(path, InStrRev(path, "\"))
    End Function
    '--------------------------------------------------------------------
    

    Here is a screenshot what i got as result with this vbscript :

    enter image description here