-2

How can I list all the running services as a list ordered by the Started time in descending ordey, by using Powershell ?

Thanks

pencilCake
  • 51,323
  • 85
  • 226
  • 363

1 Answers1

3

What have you tried? You should come quite far using Get-Service and Sort-Object.

Edit: Get-Service doesn't do start time, but there is a workaround:

[cmdletbinding()]            

param (
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
 [string[]]$ComputerName = $env:computername,            

 [ValidateNotNullOrEmpty()]
 [Alias("ServiceName")]
 [string]$Name            

)            

begin{}            

Process {            

 foreach ($Computer in $ComputerName) {
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
   Write-Verbose "$Computer is online"
   $Service = Get-WmiObject -Class Win32_Service -ComputerName $Computer -Filter "Name='$Name'" -ea 0
   if($Service) {
    $ServicePID = $Service.ProcessID
    $ProcessInfo = Get-WmiObject -Class Win32_Process -ComputerName $Computer -Filter "ProcessID='$ServicePID'" -ea 0
    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Name
    $OutputObj | Add-Member -MemberType NoteProperty -Name DisplayName -Value $Service.DisplayName
    $OutputObj | Add-Member -MemberType NoteProperty -Name StartTime -Value $($Service.ConvertToDateTime($ProcessInfo.CreationDate))
    $OutputObj
   } else {
    write-verbose "Service `($Name`) not found on $Computer"
   }
  } else {
   write-Verbose "$Computer is offline"
  }
 }            

}            

end {}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Changing the line below can allow you to do a wildcard search of the service name: $Service = Get-WmiObject -Class Win32_Service -ComputerName $Computer -Filter "Name='$Name'" -ea 0 to $Service = Get-WmiObject -Class Win32_Service -ComputerName $Computer -Filter "Name LIKE '%$Name%'" -ea 0 – Chad Rexin Nov 08 '17 at 22:08