DEV Community

Kevin Hulquest
Kevin Hulquest

Posted on

Standing on the Shoulders of Giants

I've always had an affinity for the Windows development platform. In the past I've written a plugin for SQLServer Management Studio and some PowerShell cmdlets for a storage platform. My main work has always been predominantly writing Java and Python.

Recently I had an opportunity to use my corporate Windows 10 laptop more often. Since I read all of the rave reviews about Visual Studio Code, I thought I'd reawaken my Windows development environment.

Just like any skill we acquire, if we don't use it and keep the skills sharp, then the skills become dull. But just like muscle memory, our brains quickly remember the skills that were once second nature. I have this relationship with PowerShell. I'll never be able to get over the lack of Vim keybindings but I'm always pleasantly (re)surprised at the capabilities of PowerShell. It truly blows the doors off text-based shells. Sorry Linux.

Part of my current reawakening led me to a new technique that I wanted to share with you all. The problem I faced was "I know I have a function defined to do this but I wonder what the code is behind the function?" Then I stumbled across Get-Content function:FunctionName. While this is cool, it is really just a way to remember about code that I've written in the past. The technique becomes much more powerful when it's pointed to the content written by pros.

A simple demonstration of this lay with New-Guid. Let's take a look at the result when we run Get-Content function:New-Guid.

PS [hci-monitor] > Get-Content Function:\New-Guid

    [CmdletBinding(HelpURI='https://go.microsoft.com/fwlink/?LinkId=526920')]
        [OutputType([System.Guid])]
    Param()

    Begin
    {
        [Guid]::NewGuid()
    }

As you see, this is a bit of C# that implements the Cmdlet interface. It has a pointer to documentation and arguably a (small formatting) bug since the OutputType decorator is indented too far. The function simply returns a new Guid object. I don't think the Linux folks really appreciate that this Guid isn't the text you want but a full fledged object you can forward, associate and (of course) get a text representation.

This is just a simple example. Have a look at something more complicated like Update-IscsiTargetPortal. Now you can inspect this code to get a better idea about how the pro's at Microsoft accomplish a non-trivial task. This is how you sharpen your PowerShell skills and stand on the shoulders of giants.

Until next time...

Top comments (1)

Collapse
 
tux0r profile image
tux0r

The PowerShell is seriously underrated. It grew with Microsoft's plans to include a bash and now it's mostly ignored because bash on Windows happened. :-(