Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active January 17, 2017 11:34
Show Gist options
  • Select an option

  • Save markwragg/be3b3c047401dff11c2050e3afb16920 to your computer and use it in GitHub Desktop.

Select an option

Save markwragg/be3b3c047401dff11c2050e3afb16920 to your computer and use it in GitHub Desktop.
Powershell command to see the max min and average length of cmdlets. Decided to do this after seeing how crazy long Get-WinAcceptLanguageFromLanguageListOptOut is as a cmdlet and wondering if it was the longest. Also used this to experiment with calculated properties and discovered you can use them in sort and select as well as where.
#Get stats on length of the cmdlet name such as max, min, average, sum
Get-Command | where CommandType -eq cmdlet | select -ExpandProperty name | measure length -Average -Sum -Max -Min
# See the list, sorted by length
Get-Command | where CommandType -eq cmdlet | select -ExpandProperty name | sort length
#Another way to get this statistic using calculated properties
Get-Command | where CommandType -eq cmdlet | select name,@{N='length';E={$_.name.length}} | measure length -Average -Sum -Max -Min
# See the list this way, sorted by length (we can skip the select and use the expression in sort)
Get-Command | where CommandType -eq cmdlet | sort @{E={$_.name.length}}
# You can't seem to use calculated expressions in measure-object so we seemingly can't do:
# Get-Command | where CommandType -eq cmdlet | measure @{E={$_.name.length}} -Average -Sum -Max -Min
#Calculated expressions can be used in the format- commands also, for example:
Get-CimInstance -ClassName Win32_LogicalDisk | where deviceid -eq "C:" | format-table deviceid,@{n='freespace (GB)';e={$_.freespace / 1GB};formatstring='N2'},size,@{n='description';e={$_.description};width=10}
#Format-table has a groupby param. Starts a new group every time the property changes, so may be wise to sort first.
Get-Service | Sort Status | Format-Table -GroupBy Status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment