Skip to content

Creating a PowerShell Module from a DBA’s Perspective

Problem

The last T-SQL Tuesday a lot of data professionals shared their favorite PowerShell scripts including functions, but some DBAs are new to PowerShell and don’t know how to use the functions.  So this post will cover how to create a PowerShell module so you can store the functions you just found that are useful to you as a DBA for future use, as I couldn’t find a DBA friendly blog post out there to explain how to do this for a reader of my blog.

Description of a Design of PowerShell Module components

This module is based on how I’ve seen DBATools do their setup, of course, theirs is a bit more complicated.  This is will simply allow you to collect different functions for future use.

First, you can find where your PowerShell module will be created using this command by running this command to see where your PowerShell Module will be stored at:

My first path is listed as C:\Program Files\WindowsPowerShell\Modules and recommend you use this path to create your module files in the commands in later steps.  In this folder will be creating two files, a manifest file .psd1 and .psm1 file.

Creating a Manifest file

The first file you need to create is a manifest file.  This file tells who wrote the module what version PowerShell should be used and module version.  You will need to open either PowerShell ISE or PowerShell under Run as administrator to run this command. You change the path to match the name you want the name you want your module to have along with the name of the module and author.  You may have to create the folder in your Modules folder (DBAFunctions) first depending on the security on your machine.

Creating your MODULE(PSM1) FILE

When we first created our module we used at work we put all our functions in our module file but it soon became very large and hard to find things. Then we noticed how DBATools did create their solution.  So in your folder, you created your manifest files (PSD1), create your module (PSM1) file and copy and paste the code the below.

Create a Folder to hold our Collection

Now in the folder, you created your files, create a Functions folder and save the .PS1 files you have gotten.  As an example, my reader was pulling the function I wrote from my post here.  So save the function into a file with a .ps1 extension and place into your Functions folder.  The following code will load the module and show the help if any for the function you specify:

I would also say if the author has put a header at the top, I would add and maybe edit the one they have to add the URL of where you got it from for your reference back to easily.  For example:

Using your new module

Now you are ready to use your new module and function(s).  In your favorite PowerShell IDE run the following command when you want to use one of the functions you have saved in this folder.

4 thoughts on “Creating a PowerShell Module from a DBA’s Perspective

  1. SQLDBAwithTheBeard says:

    Nice post – I so need to write up how to automate a lot of that process as per my continuous delivery tot eh powershell gallery session

    Reply
    • Tracy Boggiano says:

      Thanks I thought it complimented your T-SQL Tuesday well and I had a reader who had trouble figuring out who to use a function so I figure I would write a quick post on how to create a module for the non-PowerShell DBA’s to collect functions in from T-SQL Tuesday.

      Reply
  2. Pingback: Basics Of Powershell Modules – Curated SQL

  3. Pingback: Using Plaster To Create a New PowerShell Module | SQL DBA with A Beard

Leave a Comment