Summary: Ed Wilson, Microsoft Scripting Guy, talks about adding default values to your Windows PowerShell module manifest.
Hey, Scripting Guy! I kind of like the idea of creating a manifest for my Windows PowerShell module, but it seems like a lot of busy work. I mean, I seem to always be typing the same thing over and over. I wish there was an easy way to set default values for the module manifest. I thought about taking a blank module manifest, adding default values, and then manually editing the manifest when I needed to customize it… but that seems like a lot of work. Is there an easier way to do this?
—MT
Hello MT,
Microsoft Scripting Guy, Ed Wilson, is here. One of the great things about Windows PowerShell is that it makes it easier to do your work. In fact, that is the entire point. It is not about learning esoteric syntax or memorizing arcane commands—it is about reducing the amount of work you have to do. And that is something I love.
MT, Windows PowerShell makes it easy to specify default values for commonly used cmdlets. This is done via the default parameter values automatic variable: $PSDefaultParameterValues.
The $PSDefaultParameterValues automatic variable accepts a hash table of values. I wrote about this earlier this year in PowerShell Tips and Tricks: Using Default Parameter Values. I can do a straight-forward parameter assignment, I can use wildcard characters for all parameter with the same name, or I can add a script block that will compute the appropriate value.
The first thing I need to do is look at the syntax of the New-ModuleManifest cmdlet so I can see what parameters are available. To do this, I can use the Get-Command cmdlet and specify the –Syntax switch. This command and the associated output is shown here:
PS C:\> Get-Command New-ModuleManifest -Syntax
New-ModuleManifest [-Path] <string> [-NestedModules <Object[]>] [-Guid <guid>] [-Author
<string>] [-CompanyName <string>] [-Copyright <string>] [-RootModule <string>]
[-ModuleVersion <version>] [-Description <string>] [-ProcessorArchitecture
<ProcessorArchitecture>] [-PowerShellVersion <version>] [-ClrVersion <version>]
[-DotNetFrameworkVersion <version>] [-PowerShellHostName <string>]
[-PowerShellHostVersion <version>] [-RequiredModules <Object[]>] [-TypesToProcess
<string[]>] [-FormatsToProcess <string[]>] [-ScriptsToProcess <string[]>]
[-RequiredAssemblies <string[]>] [-FileList <string[]>] [-ModuleList <Object[]>]
[-FunctionsToExport <string[]>] [-AliasesToExport <string[]>] [-VariablesToExport
<string[]>] [-CmdletsToExport <string[]>] [-DscResourcesToExport <string[]>]
[-PrivateData <Object>] [-Tags <string[]>] [-ProjectUri <uri>] [-LicenseUri <uri>]
[-IconUri <uri>] [-ReleaseNotes <string>] [-HelpInfoUri <string>] [-PassThru]
[-DefaultCommandPrefix <string>] [-WhatIf] [-Confirm] [<CommonParameters>]
Now, I pick the parameters I want to set default values for. The following looks like what I want to assign:
Author, CompanyName, Copyright, FunctionsToExport, aliasesToExport, and VariablesToExport.
To do this, I basically need to paste my assignment line several times, so I end up with the following:
Now, it is simply a matter of type and assign. When I am finished, my code looks like this:
$PSDefaultParameterValues = @{
"New-ModuleManifest:Author" = "Ed Wilson" ;
"New-ModuleManifest:CompanyName" = "Microsoft" ;
"New-ModuleManifest:Copyright" = "2015" ;
"New-ModuleManifest:FunctionsToExport" = "*" ;
"New-ModuleManifest:AliasesToExport" = "*" ;
"New-ModuleManifest:VariablesToExport" = "*" ;
}
I can now create a new module manifest by simply passing the path for the module:
New-ModuleManifest -Path "C:\fso\mymodule.psd1"
The following shows the output after I created my default parameter values my new module:
As you can see here, I can view my module manifest in Notepad:
If I like the results, I can add the code to my Windows PowerShell profile, so that every time I open Windows PowerShell, it will create the default values.
MT, that is all there is to using custom default Windows PowerShell parameter values. 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
0 comments