Modules are added using the ImportPSModule method of InitialSessionState:
$initialSessionState = [InitialSessionState]::CreateDefault2()
$initialSessionState.ImportPSModule('Pester')
Several modules can be added with the same method. Modules can be specified by name, in which case the most recent will be used. A module may be specified using a hashtable that describes a name and version information:
$initialSessionState.ImportPSModule(@(
'NetAdapter'
@{ ModuleName = 'Pester'; ModuleVersion = '4.6.0' }
))
MaximumVersion and RequiredVersion may also be used with the hashtable.
A snap-in may be imported in Windows PowerShell using the ImportPSSnapIn method. The method requires the name of a single snap-in, and a reference to a variable to hold any warnings raised during import:
$warning = [System.Management.Automation.Runspaces.PSSnapInException]::new()
$initialSessionState.ImportPSSnapIn('WDeploySnapin3.0', [Ref]$warning)
If multiple snap-ins are required, the ImportPSSnapIn method must be called once for each.