Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: programmatically access script documentation

Is there a way to programmatically load the documentation of a .ps1 script file outside of commands like get-help? In other words, can text defined under .SYNOPSIS, .DESCRIPTION, etc. be accessed programmatically other than filtering the string output of get-help itself?

Among other things, I'm trying to find where I have gaps in documentation coverage in my script library. I'd also like to be able to display lists of certain scripts with their synopsis attached.

like image 808
UtopiaLtd Avatar asked Sep 19 '25 03:09

UtopiaLtd


1 Answers

Yes, those are all accessible. Get-Help returns (just like any other cmdlet) an object, and the default rendering of that object is what you see in the console.

However, if you pump get-help's output through format-list, like this:

get-help get-childitem | format-list

You'll get a list of name-value pairs of the properties. To get the synopsis, you can do the following:

get-help get-childitem |select-object -property synopsis

And the output:

Synopsis
--------
Gets the files and folders in a file system drive.

If your .ps1 file has no cmdlets defined in it (your comment-based help covers the whole script), get-help file.ps1|select synopsis should work. Otherwise, you'll need to "dot-source" the files to load the cmdlet definitions into memory, then use get-help as above.

like image 190
alroc Avatar answered Sep 21 '25 01:09

alroc