PowerShell Tip : Extract Comments from Script


Hello Reader!

To Review and understand scripts written by another developer,  I often find Comments helpful.

PURPOSE : 

I was looking for a way through which we can extract all comments. Just to get a skeleton view of script’s functionalities documented by the programmer. Saves me some time!

So today’s tip is about extracting all comments from a Powershell script, and it supports following items –

  1. Comment extraction from a File

    img
  2. Comment extraction from a script copied to clipboard

    2img
  3. [Updated] Includes Line number of the comment
  4. [Updated] Output matches the comment Indentation in the Powershell scriptimg

APPROACH : 

  1. Get the content of the file and Tokenize it.
  2. Filter out comments only from all Powershell tokens.
  3. [Updated] Identify the Line number  and Column position (Indentation) of each comment token
  4. [Updated] Add spaces to the output in order to match script indentation.

And a Small animation showing how it runs.

ezgif-com-video-to-gif

There could be more ways to do that like below, but I Personally trust the Parser more 🙂

tip

Hope you find the Powershell tips useful!

Prateek Singh

Advertisements

One thought on “PowerShell Tip : Extract Comments from Script

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s