PowerShell – How to Import-Module in a Runspace

There are two ways to import modules programmatically, but I’ll address your method first. Your line pipeline.Commands.Add("...") should only be adding the command, not the command AND the parameter. The parameter is added separately:

# argument is a positional parameter
pipeline.Commands.Add("Import-Module");
var command = pipeline.Commands[0];
command.Parameters.Add("Name", @"G:\PowerShell\PowerDbg.psm1")

The above pipeline API is a bit clumsy to use and is informally deprecated for many uses although it’s at the base of many of the higher level APIs. The best way to do this in powershell v2 or higher is by using the System.Management.Automation.PowerShell Type and its fluent API:

# if Create() is invoked, a runspace is created for you
var ps = PowerShell.Create(myRS);
ps.Commands.AddCommand("Import-Module").AddArgument(@"g:\...\PowerDbg.psm1")
ps.Invoke()

Another way when using the latter method is to preload modules using InitialSessionState, which avoids the need to seed the runspace explictly with Import-Module.

InitialSessionState initial = InitialSessionState.CreateDefault();
    initialSession.ImportPSModule(new[] { modulePathOrModuleName1, ... });
    Runspace runspace = RunspaceFactory.CreateRunspace(initial);
    runspace.Open();
    RunspaceInvoke invoker = new RunspaceInvoke(runspace);
    Collection<PSObject> results = invoker.Invoke("...");

Hope this helps.

Leave a Comment