PowerShell Tip : Extract Comments from Script
To Review and understand scripts written by another developer, I often find Comments helpful.
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 –
- Comment extraction from a File
- Comment extraction from a script copied to clipboard
- [Updated] Includes Line number of the comment
- [Updated] Output matches the comment Indentation in the Powershell script
- Get the content of the file and Tokenize it.
- Filter out comments only from all Powershell tokens.
- [Updated] Identify the Line number and Column position (Indentation) of each comment token
- [Updated] Add spaces to the output in order to match script indentation.
And a Small animation showing how it runs.
There could be more ways to do that like below, but I Personally trust the Parser more 🙂
Hope you find the Powershell tips useful!