September 15th, 2012

Weekend Scripter: What Does a PowerShell Module Expose?

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about identifying what a Windows PowerShell module exposes to the user.

Microsoft Scripting Guy, Ed Wilson, is here. This is it; today is the day for Windows PowerShell Saturday in Charlotte, North Carolina. If you are in the area of the Microsoft Office today, come on by and at least say hi. There are nearly 250 people attending the event, and it is like Woodstock, Burning Man, and the World’s Fair all rolled into one huge scripting geek fest. (Oh, by the way, if you are unable to attend the Charlotte, North Carolina Windows PowerShell Saturday, there is another one in Atlanta, Georgia on October 27, 2012. In the coming weeks, I will be posting pictures from the Windows PowerShell Saturday event and from the scripting slumber party (which means finding time to upload the pictures to my computer and sorting through literally hundreds of pics).

One of the cool things about Windows PowerShell modules is that they can contain a number of items. These items include aliases, cmdlets, and functions. To find these things, first use the Get-Module cmdlet to retrieve the module, and then examine the properties. A good place to start is to look at the members of the PSModuleInfo object. This is shown here. (gm is an alias for get-Member.)

PS C:\> Get-Module *management | gm -MemberType property ex*

 

   TypeName: System.Management.Automation.PSModuleInfo

 

Name                MemberType Definition

—-                ———- ———-

ExportedAliases     Property   System.Collections.Generic.Dictionary[string,Syste…

ExportedCmdlets     Property   System.Collections.Generic.Dictionary[string,Syste…

ExportedCommands    Property   System.Collections.Generic.Dictionary[string,Syste…

ExportedFormatFiles Property   System.Collections.ObjectModel.ReadOnlyCollection[…

ExportedFunctions   Property   System.Collections.Generic.Dictionary[string,Syste…

ExportedTypeFiles   Property   System.Collections.ObjectModel.ReadOnlyCollection[…

ExportedVariables   Property   System.Collections.Generic.Dictionary[string,Syste…

ExportedWorkflows   Property   System.Collections.Generic.Dictionary[string,Syste…

When I see that there are properties for the various things a module might export, I want to examine them. First, notice that these are all collections. This has implications for the way that information returns from the Get-Module cmdlet. I like to see what properties are returning anything at all, so an easy way to do that is to pipe the results to the Format-Table cmdlet as shown here. (ft is an alias for Format-Table, and I am choosing properties that begin with the letters ex.)

PS C:\> Get-Module *management | ft ex*

 

ExportedFu ExportedCm ExportedCo ExportedVa ExportedAl ExportedW ExportedF ExportedT

nctions    dlets      mmands     riables    iases      orkflows  ormatFile ypeFiles

                                                                 s

———- ———- ———- ———- ———- ——— ——— ———

{}         {[Add-C… {[Add-C… {}         {}         {}        {}        {}

It appears that only two of the properties return information for the *management module, the ExportedCmdlets and the ExportedCommands properties. Let me hone in on them by using the command shown here.

PS C:\> Get-Module *management | ft exportedc*

 

ExportedCmdlets                            ExportedCommands

—————                            —————-

{[Add-Computer, Add-Computer], [Add-Con… {[Add-Computer, Add-Computer], [Add-Co…

I see that both properties, ExportedCmdlets and ExportedCommands return exactly the same information. Therefore, I will focus my efforts on ExportedCmdlets. Here is the command I use.

PS C:\> Get-Module *management | Select ExportedCmdlets

 

ExportedCmdlets

—————

{[Add-Computer, Add-Computer], [Add-Content, Add-Content], [Checkpoint-Computer, …

 

PS C:\>

Bummer! I do not see all of the exported cmdlets. The reason goes back to the fact that we have a collection, and we are limited by the value of the automatic preference variable, $FormatEnumerationLimit (it is set to 4 by default. (For more information about this variable, see Change a PowerShell Preference Variable to Reveal Hidden Data.) Without changing the value of $FormatEnumerationLimit, the easiest way to see all of the cmdlets is to add the ExpandParameter parameter to the Select-Object command. This is shown here.

Get-Module *management | Select -expand ExportedCmdlets

The command and associated output are shown in the image that follows.

Image of command output

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.