Reporting SQL Server Version Information

Pulled together some scripts to make a cmdlet which returns version information from one or more SQL servers. Remember to set the SQLServer parameter value correctly. It will be different depending on whether you are using a named instance. You can find a copy of the source code at my gitlab repository.

I’ve used Invoke-Command in the script so that the function can also be run remotely against other systems.

function Get-SQLVersion

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Computer name')] [string] $Computername = $env:computername,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Instance')] [string] $SQLServer

        Invoke-Command -ComputerName $Computername -ScriptBlock {
            $SQLquery = @'
SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')

            $SqlConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection
            $SqlConnection.ConnectionString = "Server=$using:SQLServer;Database=Master;Integrated Security=SSPI;"
            $SqlCmd = New-Object -TypeName System.Data.SqlClient.SqlCommand
            $SqlCmd.CommandText = $SQLquery
            $SqlCmd.Connection = $SqlConnection
            $SqlAdapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter
            $SqlAdapter.SelectCommand = $SqlCmd

            $DataSet = New-Object -TypeName System.Data.DataSet
            $nSet = $SqlAdapter.Fill($DataSet)
            $Tables = $DataSet.Tables
            $arrayVersion = ($($Tables.Column1).Split('.'))
            [string] $SQLVersionNumber = "$($arrayVersion[0]).$($arrayVersion[1])"

            Switch -Wildcard ($SQLVersionNumber) {
                    $versionName = 'SQL Server 2016'
                    $versionName = 'SQL Server 2014'
                    $versionName = 'SQL Server 2012'
                    $versionName = 'SQL Server 2008 R2'
                    $versionName = 'SQL Server 2008'
                    $versionName = 'SQL Server 2005'
                    $versionName = 'SQL Server 2000'
                    $versionName = 'SQL Server'


            $hash = @{
                DisplayName   = "$versionName $($Tables.Column2) $($Tables.Column3) $($Tables.Column1)"
                Name          = $versionName
                Version       = $Tables.Column3
                ServicePack   = $Tables.Column2
                VersionNumber = $Tables.Column1

            New-Object -TypeName PSobject -Property $hash

With this complete, we can use the cmdlet, and expect output similar to the format below.

SQL - VersionInformation


Using the MouseEnter and MouseLeave Events in PowerShell Studio

Default Winforms on their own can sometimes look a bit bland, but there are some things we we can do, which do not require much coding, that can really enrich the GUI experience.

The MouseEnter and MouseLeave events can be easily overlooked when writing a forms application. A MouseEnter event is raised when the mouse pointer moves over an area, or to be more correct, a specific controls region. The MouseLeave event is the opposite. It is raised when the pointer is moved outside of the region of a control.

You can take advantage of these events in many ways. In this post, we’ll use them to highlight the control over which the pointer is, and display descriptive text associated with that control. The style is meant to be not unsimilar to some of the Microsoft installation dialogs. 🙂

NB If you wish, you can skip the steps below by downloading the source files from my GitHub repository.

Create the Forms Project

First of all, create a new forms project. Let’s call it MouseEnter. Now add the following controls, setting their properties as mentioned below.

Control Type Name Text BackColor Font
Form frmMouseEnterDemo MouseEnter demo White default
Panel panel1 na 1,36,86 default
Label labelDescription Description na Size 10, Bold
Label LabelOptions Options na Size 10, Bold
Label LabelOverview Overview na default
Label LabelMouseEnterApplication MouseEnter Application na default

Position labelDescription so that it occupies the left half of the form, underneath the blue panel.

Position these last three labels on the right hand side of the form, with the Options label at the top, followed by the other two directly under it.

Your form layout should now look something like this :

MouseOver - Initial Form Setup

With the labelDescription control positioned as we want it, clear its the text property, so that it is blank.

Now we need to add code for five events :

  • form loading
  • mouse enters overview label region
  • mouse leaves overview label region
  • mouse enters mouse enter application region
  • mouse leaves mouse leave application region

Writing the Event Code

We’ll register event handlers for the above actions by the process below, and then paste in the event code that is further down this page.

  • Select the properties panel, and the events (lightening) button
  • Double click on the form
  • Go back to the form view, select the overview label, double click on MouseEnter.
  • Do this also for the MouseLeave event
  • Repeat the above for the mouseenterapplication label.

Once you have completed, this your form should show a set of entries for the events we have selected. These will be empty. Select the text, and clear it.

Select the text below, copy it into the clipboard, and then paste it into the code window.

	$labelOverview.ForeColor = 'Blue'
	$labelDescription.Text = $overviewText

	$labelOverview.ForeColor = 'Black'
	$labelDescription.Text = $defaultDescriptionText

	$labelMouseEnterApplication.ForeColor = 'Blue'
	$labelDescription.Text = $applicationText

	$labelMouseEnterApplication.ForeColor = 'Black'
	$labelDescription.Text = $defaultDescriptionText

	$script:defaultDescriptionText = 'Welcome to the MouseEnter example! This example demonstrates how we can use this.'
	$script:overviewText = 'When the mouse pointer enters an area on which a control resides, a MouseEnter is raised. After the mouse departs from that specific controls area, a MouseLeave event occurs'
	$script:applicationText = 'This application provides an example of how we can use the MouseEnter and MouseLeave events to enrich the GUI experience on a form'
	$labelDescription.Text = $script:defaultDescriptionText

This sets the text that  to be shown when a control’s region is entered or left by the mouse pointer, and also other actions to be performed in these circumstances. When the pointer enters a control’s region, we set its text is set blue, and the label on the left hand of the screen displays some descriptive text. On startup, or when a pointer leaves a controls region, this description is reset back to the default welcome text.

mouseover - part 2 - form code

Rearrange your form as you wish.


Now let’s run our project to see the results.

Overview - MouseEnter

Overview Mouse Enter

Overview - MouseLeave/Not in any region

Overview MouseLeave

When the pointer is positioned over one of the options, the font changes to blue, and the descriptive text on the left hand side also changes. When the pointer moves outside the controls region, the default text is displayed, and the font set back to black.

And a video which highlights what’s happening a bit better. Apologies for the poor quality, I’m still wrestling with uploads to YouTube at the moment :

As always, comments and feedback welcome. Thanks for reading.




URL Shortening

Love it or loathe it, URL shortening has been with us a while now and can certainly be handy. TinyURL are one such company to offer this service. Nicely for us, we do not need to register in order to use their API, and yet nicer still is that we can use it simply by entering a standard format of URL.

Before we see how we can use PowerShell to automate this process, let’s take a look at the format of URL that we need to use with TinyURL.

Where targetaddress refers to the URL that you wish to shorten.

And that’s it.

Let’s say we wanted share a link containing information about this years PowerShell Summit Europe event in Stockholm. The full length URL for this is :

If we wanted to get the TinyURL equivalent of this, we’d use the following URL, pasting it into the address bar of our browser.


For making this happen via PowerShell, Invoke-WebRequest is our friend. All we need to do is provide the required address via the Uri parameter, and the Content property of the returned HtmlWebResponseObject will contain its shortened equivalent.

So for the case of the above we’d be using a command (note the pipeline symbol) of the type :

Invoke-WebRequest -Uri '' |
Select-Object -ExpandProperty Content

And can expect to get :


I’ve put together a cmdlet called Get-TinyURL for doing this. At its simplest, you can run it with the Uri parameter, and it will return a PSObject containing the original full address and its shortened equivalent.

Get-TinyURL -Uri ''



It’s also been bulked out a bit to give some extra functionality, such as being able to read from and write to the clipboard if we want. With both options enabled, we can copy a full address into the clipboard, run the cmdlet, and automatically have the shortened URL available for pasting wherever we want it next.

Navigate to desired URL and copy it to the clipboard

Get-TinyURL -ReadClipboard -WriteClipboard

Run the required command

pseuemail Paste where required

The code used is listed below, and will also be posted on GitHub in due course.

function Get-TinyURL
        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True,ParameterSetName = 'URI')] [string] $Uri,
        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True,ParameterSetName = 'ReadClipboard')] [switch] $ReadClipboard,
        [Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)] [switch] $WriteClipboard = $False
        If ($ReadClipboard -or $WriteClipboard) 
            $null = Add-Type -AssemblyName System.Windows.Forms
        If ($ReadClipboard) 
            $Uri = []::GetData('System.String')
        $tinyURL = Invoke-WebRequest -Uri "$Uri" | 
        Select-Object -ExpandProperty Content
        If ($WriteClipboard) 
        $hash = @{
            Uri     = $Uri
            TinyURL = $tinyURL
        New-Object -TypeName PsObject -Property $hash