Introduction to Powershell Modules w/ Advanced Functions

          If you been using powershell you heard these terms Modules and Advanced functions . What are they ? Why are they important ? Why would I want to use them ? Do I need to use them ? As you can see allot of questions.

          Modules have replaced Pssnapins in V1 which was compiled code usually C#  that gave powershell more functionality .This was a way to add more cmdlets . An example of this is System Center Operations Manager (SCOM) pssnapin . When you install the console along with powershell you get the pssnapin for SCOM which gives you a bunch of cmdlets.

          Now with powershell V2 we have what are called modules . These modules can be any function that you have written in powershell. That is the beauty of modules they are basically scripted cmdlets that you create . Now anyone can create them. You don’t need to fire up a compiler and bang out C# code to get the extra functionality you need .So if you take any function your written in Powershell and changed the extension to .psm1 then you have a module .

           They are important because they can save you time by re-using specific scripts to do repetitive tasks . When you import your module its loaded into your powershell session sort of like dot sourcing your script. First you need to know where are the modules located . To find out where your modules folder is located enter this command in your powershell command prompt .

dir env:psmodulepath

psmodulepath is an environment variable. Please see this blog for more information

You should see something like this …

Name                           Value
—-                           —–
PSMODULEPATH                   C:\Documents and Settings\username\My Documents\WindowsPowerShell\Modules;

There is also a location in your %windir%\system32\Windowspowershell\modules directory.


You probably be using the user specific directory.

So now that you know where the modules are here is an example of a module I created :

I will call it Get-OSInFo.psm1

Here is what it looks like using Advanced Functions:


Function Get-OsInfo {

 Gets basic Os information Using WMI OperatingSystem Class.
 Gets basic Os information Using WMI OperatingSystem Class.
.Parameter ComputerName
 Name of the of the Computer you want info on.

 PS> get-OsInfo -computer serverA
NAME:      Get-OsInfo
AUTHOR:    Name
LASTEDIT:  05/26/2009


[string]$computerName = “localhost”

gwmi -class Win32_OperatingSystem -Computer $computername


            Advanced Functions is new for V2 . What makes them so nice to use is the built in help . Notice the syntax. Also notice the parameter section . This is a new way to specify parameters. Note that I have a parameter called Computername default to local host and it is mandatory. You can go crazy with parameters but this is just an intro so more to come in the future . The last portion is the standard WMI call to OperatingSystem .


Now lets put this all together 🙂

Place this file in your modules folder but create a folder called get-osinfo and put the psm1 file in there. I use the same name as the script for the folder . (I am not sure if that is necessary but it is best practices ).


Now that we have the module file in the correct place open up your powershell command prompt.

We are going to import the module …

import-module get-osinfo.psm1

Like I said before this is like dot sourcing a script (. .\scriptname.ps1). Now that we imported it lets check it out ..

Before we do I wanted to point out that you can have an umlimited number of functions in this psm1 file . So what if you got a bunch of functions for a third party how would you know what functions are in this file ?

A somewhat easy way would be to use this command (This is after you imported the module )

get-command -commandtype Function

CommandType     Name                                                     Definition
———–     —-                                                     ———-
Function        A:                                                       Set-Location A:
Function        B:                                                       Set-Location B:
Function        C:                                                       Set-Location C:
Function        cd..                                                     Set-Location ..
Function        cd\                                                      Set-Location \
Function        Clear-Host                                               $space = New-Object System.Management.Au
Function        D:                                                       Set-Location D:
Function        Disable-PSRemoting                                       …
Function        E:                                                       Set-Location E:
Function        Enable-PSRemoting                                        …
Function        F:                                                       Set-Location F:
Function        G:                                                       Set-Location G:
Function        Get-OsInfo                                               …
Function        H:                                                       Set-Location H:

You can see we see our get-Osinfo function listed .


Now that we know that is the function we need …we need to know how to run it (Remember the built in help for the function we created ?? )

Well lets get help the same way you would get help on any of other function or cmdlet.

help get-osinfo -det


    Gets basic Os information Using WMI OperatingSystem Class.
    Get-OsInfo [-computerName] [<String>] [-Verbose] [-Debug] [-ErrorAction [<ActionPreference>]] [-WarningAction [
    nce>]] [-ErrorVariable [<String>]] [-WarningVariable [<String>]] [-OutVariable [<String>]] [-OutBuffer [<Int32>
    Gets basic Os information Using WMI OperatingSystem Class.
        Name of the of the Computer you want info on.

        This cmdlet supports the common parameters: -Verbose, -Debug,
        -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
        -OutBuffer and -OutVariable. For more information, type,
        “get-help about_commonparameters”.

    ————————– EXAMPLE 1 ————————–


    PS> get-OsInfo -computer serverA
    To see the examples, type: “get-help Get-OsInfo -examples”.
    For more information, type: “get-help Get-OsInfo -detailed”.
    For technical information, type: “get-help Get-OsInfo -full”.


Isn’t that great !!!

Lets run it …

Get-Osinfo -computername ServerA

(Notice how the parameter variable computername is used …)

You can even pipe this to the select cmdlet  like so

Get-Osinfo -computername ServerA | select Name,Organization,BuildNumber


To sum up … Modules helps you create specific functions to help you with different tasks . Microsoft made these scriptable so that any one can create them . By using advanced functions you get the built in help and special parameter settings for your functions .

It doesn’t stop here … there are also module manifests . Also you can enable different functions in your modules for lets say security reasons . This was just an introduction.


I hope you enjoyed reading this blog 🙂