PowerShell ISE goes Agile!

The PowerShell Integrated Scripting Environment (ISE) just got posted to the PowerShell Gallery, official post.

ISE has shipped in the PowerShell box for years, meaning, the only way we got new features or fixes was when a new version of PowerShell shipped.

Starting with the new PowerShellISE-preview, you’ll see new features perhaps on a monthly basis.

Plus, it runs side by side with the the built-in ISE, so you won’t be disrupted and will always a stable release to fall back on.

But wait, there’s more! You get to add to and vote on the backlog. Head over to the PowerShell UserVoice site and have at it.

I did a down and dirty web scraping of the 59 open items for ISE, here are the top 10:

This is great news. We get features and fixes that much sooner, and the add-on model for ISE is the first scheduled set of improvements. That’s terrific news for everyone who wants to customize the editing experience.

Note: This first preview release only works with PowerShell v5, is English-only and existing add-ons could have issues.

So, download it, kick the tires, and head on over to the UserVoice site and vote early and often. Don’t forget to add what you want to see in ISE too!

Visual Studio Code

While we’re on the topic of editors, Microsoft’s new modern editor Visual Studio is available and has PowerShell support. Plus, the VS Code, PowerShell Editor Services, and vscode-powershell extension are on open source and up on GitHub.

Folks like Keith Hill, Adam Driscoll and myself have already contributed features.

Options are always good. Having multiple editors tuned to for different purposes is great.

Convert JSON or CSV to a PowerShell Class

There’s a new keyword in PowerShell v5.0, it’s for creating classes directly in your PowerShell scripts. Check out Introduction to PowerShell 5 Classes.

Verify Incoming Data

One application of PowerShell classes is the ability to simplify verification of incoming data. For example, you may have comma separated input that looks like this:

Line 3 clearly has bad data. If you use ConvertFrom-Csv, it will happily create PowerShell objects on the fly but both the name and age property will be defined as strings, and 10a is a string. We need a little more safety.

Here is a class Person that strongly types the properties name as string and age as an int.

Putting this together let’s you detect data errors early.

Running the above generates this error.

This is a great way to do pre-flight checks on incoming data to determine if there are issues.

Expand the DataSet

What if the input data has more properties:

Working up a PowerShell class by hand for this can be tedious and error prone. So, let’s automate it with ConvertTo-Class

ConvertTo-Class

ConvertTo-Class is a PowerShell module I published, and it’s on the gallery. It automates creating a PowerShell class from either CSV or JSON text data. ConvertTo-Class determines both the name of the property and the actual data type of the data.

Quick, Easy and accurate!

ConvertTo-Class infers the correct data type for the properties age,zip, and rent.

The class is ready to go. You can save it to a file and dot source it or you can do an Invoke-Expression on it and use it immediately.

Auto Detects JSON Too

The same ConvertTo-Class function atuo detects JSON and produces the same class as it did with the CSV data.

Here is the same class. Notice you can use the -ClassName parameter to override the default class name of RootObject.

Plus, ConvertTo-Class Handles Multiple Classes

Here is sample JSON that has multiple classes. ConvertTo-Class detects and generates each class with it’s properties.

Here’s the code generated PowerShell classes, wired up and with the correct data types.

Generate C# or PowerShell Classes

PowerShell class syntax is the default output. You can also generate classes for use in C# with the -CodeGen CSharp parameter.

In an upcoming post, you’ll see how you can easily specify your own code generation “rules” to create constructs for other languages or purposes.

In Action Video

I’ve also published two PowerShell scripts that let you paste JSON or CSV to classes from the clipboard in either the console, using PSReadline or from ISE via an addon.

image

Grab the PowerShell

You can get ConvertTo-Class from the PowerShell Gallery or on my GitHub repo.

More Visual Studio Code Extensions – Rendering Markdown through Pandoc

Markdown is a fantastic way to overcome the ceremony of writing a document. From blog posts, to readme files and publishers let authors write their book in markdown. Once you’re done writing, the last step is to render the markdown in a format for the target audience. If it’s a comment on GitHub/Stackoverflow, it’s automatically rendered.

If you using a text editor, you might drop down to the command line or context switch to another tool.

This Just got easier in Visual Studio Code

This Visual Studio Code extension lets you easily render markdown files as a pdf, word document or html file.

You need to install Pandoc – a universal document converter.

Two ways to run the extension. You need to have a markdown file open.

  1. press F1 on Windows (cmd+shift+P on Mac), type pandoc, press Enter
  2. Or – press the key chord ctrl+k then p

Then choose from the list what document type you want to render and press enter (you can also type in the box rather than cursor around).

Getting the Extension

The vscode-pandoc extension is published on the gallery. You can install the vscode-pandoc from the palette.