(Not a bug) Sort command not working correctly - Solution given - by Vandrey Trindade

Status : 


Sign in
to vote
ID 794099 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 7/16/2013 5:24:54 AM
Access Restriction Public



I'm doing the PowerShell 3.0 MVA and I saw that in the command below, where the services should be listed first sorted by the status column then by the displayname column, it is sorted in descending order by default. When I ask to sort it using only the displayname column it works correctly.


Get-Service | Sort-Object status,displayname


Status   Name               DisplayName
------   ----               -----------
Stopped  hidserv            Acesso a Dispositivo de Interface H...
Stopped  wmiApSrv           Adaptador de Desempenho WMI
Stopped  AdobeFlashPlaye... Adobe Flash Player Update Service
Stopped  MMCSS              Agendador de Classes de Multimídia
Stopped  napagent           Agente de Proteção de Acesso à Rede
Stopped  p2psvc             Agrupamento de Rede de Mesmo Nível
Stopped  RpcLocator         Alocador Remote Procedure Call (RPC)
Stopped  aspnet_state       ASP.NET State Service
Stopped  SDRSVC             Backup do Windows
Stopped  PeerDistSvc        BranchCache
Stopped  SensrSvc           Brilho Adaptável
Stopped  SCardSvr           Cartão inteligente
Stopped  WebClient          Cliente da Web
Stopped  Wecsvc             Coletor de Eventos do Windows
Running  AdobeARMservice    Adobe Acrobat Update Service
Running  Schedule           Agendador de Tarefas
Running  PolicyAgent        Agente de Diretiva IPsec
Running  ProtectedStorage   Armazenamento Protegido
Running  CscService         Arquivos Offline
Running  stisvc             Assistente de aquisição de imagens ...
Running  AudioSrv           Áudio do Windows
Running  iphlpsvc           Auxiliar de IP
Running  lmhosts            Auxiliar NetBIOS TCP/IP
Running  wscsvc             Central de Segurança
Running  RpcSs              Chamada de procedimento remoto (RPC)
Sign in to post a comment.
Posted by Vandrey Trindade on 8/22/2013 at 7:31 AM
Thanks for your time!
Posted by mhmhmhmh on 7/16/2013 at 10:09 AM
I guess that this is by design.

Just do a
(get-Service)[0] | get-member
and you'll find out, that Status is not a String, but a System.ServiceProcess.ServiceControllerStatus, which is an enum (see http://msdn.microsoft.com/de-de/library/system.serviceprocess.servicecontrollerstatus.aspx for documentation).
As Stopped is defined as 0x00000001 and Running is 0x00000004, Stopped will be placed before Running when sorting ascendingly.

If you want to sort by the Status-String, you can simply cast the column to String:
Get-Service | Sort-Object {[string]$_.Status}