Strange PowerShell Notebook Behavior When Comments Are Involved

This is not the blog post I was intending to write. I started off writing about a different topic, and had some code that I wanted to share in a PowerShell Notebook. Getting that notebook to behave like it should have, however, is another story entirely and worthy of its own post – this one.

PowerShell Logo

PowerShell Notebooks are a great new feature in Azure Data Studio, first becoming available in the November 2019 release. Like SQL notebooks, PowerShell notebooks are based on Jupyter Notebooks format, which are interactive documents containing text and executable code blocks.

Having some working PowerShell code that I wanted to share along with explanations and examples, I created a PowerShell Notebook. The only problem was my functions would never initialize. Actually they would never stop initializing – I would run the cell they were defined in, and it would just keep running forever.

After several days of not being able to figure this out, I reached out to a true master, gentleman, scholar, and friend: Rob Sewell (@sqldbawithbeard). Rob took a look at this, and in under 10 minutes of experimentation, discovered that my personal habit of placing comments at the end of functions was causing the problem. Whenever I have a closing bracket, be it at the end of a function, class, file, or even a for loop, I like to make a comment to note what scope is ending. This is completely valid PowerShell, and in a .ps1 file it works just fine.

1Function Do-Something {
2   5
3}# comment at end of Do-Something
4Function Do-SomethingElse {
5   8
6}# end of Do-SomethingElse

PowerShell Notebooks, however, don't like this for some reason. And to experiment further, I have none other than a PowerShell Notebook to demonstrate several different scenarios:

Think this is strange?

I do. And Rob does. So I've filed an issue with the Azure Data Studio team.

EDIT: It turns out this issue is fixed in a later version of the PowerShell kernel. See how to update it here.