Skip to content

Instantly share code, notes, and snippets.

@StartAutomating
Created October 28, 2025 23:06
Show Gist options
  • Select an option

  • Save StartAutomating/fb68aa34038055d8f4a07f8a3151456f to your computer and use it in GitHub Desktop.

Select an option

Save StartAutomating/fb68aa34038055d8f4a07f8a3151456f to your computer and use it in GitHub Desktop.
Gist get me the Doc Ratio
function DocRatio {
<#
.SYNOPSIS
DocRatio
.DESCRIPTION
Gets the ratio of comments to non-comments in a script
.EXAMPLE
docratio docratio
#>
$allInputAndArgs = @($input) + $args
foreach ($thing in $allInputAndArgs) {
$originalThing = $thing
if ($thing -is [string] -and (Test-Path $thing -ErrorAction Ignore)) {
$thing = Get-Item -Path $thing
}
if ($thing -is [string] -and $(
$thingFunction = $ExecutionContext.SessionState.InvokeCommand.GetCommand($thing,'Function')
$thingFunction
)) {
$thing = $thingFunction.ScriptBlock
}
if ($thing -is [IO.FileInfo] -and $thing.Extension -eq '.ps1') {
$thing = Get-Command $thing.FullName
}
if ($thing -is [Management.Automation.ExternalScriptInfo]) {
$thing = $thing.ScriptBlock
}
if ($thing -is [ScriptBlock]) {
$tokens = [Management.Automation.PSParser]::Tokenize("$thing", [ref]$null)
$totalLength = 0
$commentLength = 0
foreach ($token in $tokens) {
$totalLength += $token.Length
if ($token.Type -eq 'Comment') {
$commentLength += $token.Length
}
}
if ($totalLength) {
$ratio = $commentLength / $totalLength
@{$originalThing=$ratio}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment