Nemanja Popovic

How to create a simple SQL Server database login dialog using PowerShell

January 30, 2017 by

In this article, we’ll be creating a SQL Server login form, as shown in the example below, using PowerShell to accept user input. We’ll show the code behind the form, how to capture the user input and utilize it:

First, let’s create the function that will be used for creating custom input form:

Now, the first thing is to create the input form:

Next, let’s define handling of the Enter and Escape keys to perform as OK and Cancel:

The next part is used to create the OK button, define its size, position, title and behavior:

Also, let’s create the Cancel button, define its size, position, title, and behavior:

The following loop dynamically creates labels and text boxes:

Now, let’s create dynamic label, define its size, position and set the associated parameter as its text:

Also, let’s create dynamic text box, define its size and position:

If the input parameter contains sign (*) the text box associated with the parameter will have obscured text:

The last part will show the input form, focus the cursor in the first text box. If OK button is clicked all entered values will be returned, otherwise the script execution will be stopped:

With this last part, the Show-InputForm() function is completed and we can move with providing examples.

In the following line, we’ll load .NET types necessary for working with Windows forms:

Here is an example of using the Show-InputForm() function and defining variables for entering source and destination SQL Server database credentials:

Once we hit “OK” we’ll collect the information input and use it to build a connection string to connect to SQL Server using the following part in the PowerShell script:

You can download the complete PowerShell script from here.

Useful link

  • Prompt for user input in PowerShell
  • SQL Database development

    About Nemanja Popovic

    Nemanja is an IT enthusiast who enjoys reading books, playing and watching soccer and basketball, and traveling. He uses his spare time on tuning and styling of his car, playing video games, and fishing. Nemanja is currently working for ApexSQL LLC as a Software sales engineer. He’s specialized in SQL database schema and data comparison and synchronization, and he helps customers with technical issues, providing DEMO presentations, and does the quality assurance for ApexSQL Data Diff and ApexSQL Diff. View all posts by Nemanja Popovic