We’re going a little more into turning a script into a module.  And again, don’t scare yourself off!  They are easy to deal with!

So here today,  I’ll go the other way around.   I’m going to take a simple script with a few built in Variables and modify it into a module that I can type IN those variables every time.

Here’s one I use on a regular basis.   It’s a simple thing that uses the Quest Active Roles and pulls up a list of users created in a specific date range under a particular OU.



connect-qadservice -service ‘contoso.local’

get-qaduser -searchroot ‘contoso.local/Users’ |  where { ($-.Creationdate -ge $startdate) -and ($-.Creationdate -le $enddate) } |  select-Object Name


Not a very fancy script, but one I find handy.     It users in a particular OU in Active Directory, and lists those created within a particular date range.

Now rather than going in to the script EACH and EVERY time, wouldn’t it make more since to just turn it into a useful command and TYPE in what you wanted?

That’s a module.

So all we have to do is specify the variables that will PASS THRU from the command line.   Last time it was the Variable called ‘$Name’ that was sent to be echoed to the screen.  Yes, it was a lame but simple example.   This time we’re going to pass THREE variables to the module.  $startdate, $enddate, and $ou so that we can simply call up our module now and have as a useful regular command in the Shell environment.

So this script as a module looks like *THIS*


New-Module -scriptblock {

function ListUsers ($startdate, $enddate, $ou)


connect-qadservice -service ‘contoso.local’

get-qaduser –searchroot “contoso.local/$ou” |  where { ($-.Creationdate -ge $startdate) -and ($-.Creationdate -le $enddate) } |  select-Object Name


Export-ModuleMember -function ListUsers



As you can see all we’ve done is take the variables we want to pass through from the command line and put them into a function and then Export that function at the end.   That’s it.

Once you execute this script you will now have a new command call “LISTUSERS” you can from the Powershell environment.  Note, that unlike a Shell script you do NOT have to specify a path to the filename to make it execute.   And you can easily add in the required parameters.   And just like a Shell script, you can check for the input values and compare that they meet Syntax or whatever you choose.

Note the “ModuleName ‘LISTUSERS’ is derived directly from the original function name.  Because we “exported” that function.

Next time we’ll play a little deeper like showing how to have the variables default and EVEN how to have you module check for dependancies (like it’s needed snapins) before running.

‘Til Next Time

The Energized Tech