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!

Optical Character Recognition

Author of “PowerShell Guide to Python“, “Windows Subsystem for Linux (WSL)” and currently writing the most awaited book: “PowerShell to C# and Back” !


Subscribe to our mailing list

* indicates required