DCSIMG
September 2008 - Posts - This blog has moved

This blog has moved

September 2008 - Posts

Document your PowerShell functions on the fly

So, you’ve filled your PowerShell profile with dozens of useful functions and aliases. You use a few of them very often. But most of them are useful only once in a while. Now isn’t it annoying to have to open a ps1 file and go over all function definitions just to find the function name for a script that would sometimes take even less time to just write anew?

Imagine you could use something like “Get-Help” or “Get-Command” to remind you of functions that you wrote a while back.

Why imagine? Let me introduce you to the DocProvider script.

First watch this video to get you enthusiastic.

Now get this file and put it in your PowerShell Profile directory.

Add the following lines in every script file containing documented function definitions (don’t know what documented functions are? it’s ok. read on).

# Initialize the doc provider

. "$(Split-Path $profile)\DocProvider.ps1" # Dot-source the functions from the DocProvider file

LoadDoc $MyInvocation.MyCommand.Path # Load the documentation from current file.

 

Now it’s time to actually document the functions.

This version of DocProvider only supports function description. I might add parameters description later on. Ok, so add the documentation attributes to every function in your script file. Follow the example below:

function   mem { #@Top 10 memory hoggers@

Get-Process | sort PrivateMemorySize | select PrivateMemorySize,Description,Path -Last 10

}

Run or dot-source the modified file and type “doc” to access full list of documented functions (see, told you you’d know what that phrase meant) or “doc function” to get just the documentation for the specified function.