Invoking a Template
Synopsis
How to find available templates and create a file/project from it.
Introduction
The PSModuleDevelopment templating system has a two-stage approach:
- First, the raw template files - the contents you want to generate - get recorded into a Template.
- Then, when you want to create a project from that template, you invoke that recording.
This guide deals with step 2, you can find more information on how to record your own templates in other guides (1 | 2).
Discovery
Templates can come from a few different sources:
- Default templates that come with PSModuleDevelopment
- Templates you created yourself (1 | 2)
- Templates that come with another module
- Templates provided by your organization
So how do we find what templates are available to us?
Get-PSMDTemplate
Or - if you must know exactly where they come from - you can display the Path:
Get-PSMDTemplate | Format-Table Name, Path
Templates can come in different versions, so getting a full list might be useful:
Get-PSMDTemplate -TemplateName function -All | ft Name, Version, Path
Invocation
When invoking a template, we will create a project / file based on what was originally recorded into the template. This will:
- Prompt for all missing parameters
- Execute all the scriptblocks and remember the results
- Create all the folders, inserting parameters into their name where placeholders were found during recording
- Create all the files, inserting parameters & script results into their name and content where placeholders were found during recording
The command to do that is Invoke-PSMDTemplate:
Invoke-PSMDTemplate MiniModule
This is going to invoke the template in the current path. We can of course change that:
Invoke-PSMDTemplate MiniModule -OutPath C:\Temp
Rather than invoking a template by name, we can also invoke it by object:
Get-PSMDTemplate function -RequiredVersion '2.0.1' | Invoke-PSMDTemplate
Provide template parameters during invocation
If we don't want to be prompted for missing parameters, we can pre-provide them during the function call:
Invoke-PSMDTemplate MiniModule -Name MyProject -Parameters @{ Description = "Example Module" }
As it is so common, the -Name parameter for templates is offered as a direct parameter on the command.
For all others, you either provide them in the -Parameters hashtable as shown above or define a default value.
No New Folder
By default, templates recorded from a project will be created in a subfolder named for the template parameter Name (if your template does not use that parameter, it will still be asked for, just for this folder).
If you do not wish to create a subfolder - for example, when you are in a newly created git repository and want the project contents directly in that - you can prevent the creation of that folder:
Invoke-PSMDTemplate MiniModule -NoFolder