Home > Software design >  Count instances of string output
Count instances of string output

Time:06-22

I'm trying to count versions of a specific DLL on our servers. Finding what version we have is fairly easy, but I want a rolled up count of each one. This is what I have so far:

gci System.Web.Mvc.dll -Recurse -ErrorAction SilentlyContinue | 
select-object -ExpandProperty VersionInfo | 
Select FileVersion | 
sort-object { [string]$_.FileVersion }

So if the above output:

1.2.3.4
1.2.3.4
1.2.3.4
5.6.7.8
9.0.1.2
9.0.1.2

I'd want:

1.2.3.4    3
9.0.1.2    2
5.6.7.8    1

Any idea how I would do that?

CodePudding user response:

One way to do it is:

(Get-ChildItem -Path <top-level-folder> -Include '*.dll' -Recurse).VersionInfo.FileVersion | 
    Group-Object $_ -NoElement |
    Sort-Object Count -Descending

Which, on a random directory on my machine, gives:

Count Name                     
----- ----                     
   12 4.700.19.56404           
    6 6.8.0.11012              
    6 1.0.0.0                  
    5 6.0.622.26602            
    4 2.1.1.0                  
    3 2.0.20168.4              
    3 6.3.1.0                  
    1 2.8.26.1919              
    1 6.0.222.6406             
    1 4.21.1.0                 
    1 1.2.3.0                  
    1 4.6.26919.2

Change the <top-level-folder> and the -Include to suit your situation.

  • Related