Unable to debug why ImportPSModule fails when calling Powershell cmdlets from C#
<p>I am trying to call the "Connect-MsolService" cmdlet from C# but I am getting a "... 'Connect-MsolService' is not recognized as the name of a cmdlet ..." error. Any pointers on how to debug this problem would be appreciated.</p>

<p>I based my project on the office dev center sample <a href="https://code.msdn.microsoft.com/office/Office-365-Manage-users-by-cfc96d5e" rel="nofollow">Office 365: Manage users by calling Windows PowerShell cmdlets from C#</a> which first does a ImportPSModule of the "MSOnline" module.</p>

<p>When I open the Office Dev Center sample in VS 2015 is works as expected but when I create my own project it does not. I am really struggling to get to the bottom of why I get the error in in my project but not in the sample. What I have done so far is</p>

<ul>
<li>have both projects (sample and mine) in the same solution</li>
<li>reduced both projects down to console applications removing all unnecessary dependencies and references</li>
<li>The sample had a hard reference to System.Management.Automation which I changed to a nugget reference (both worked) when I tried each in my project still got the error</li>
<li>Tried basic powershell commands (e.g. Get-Process) in my project and everything worked</li>
<li>Tried to use ImportPSModulesFromPath and a full path to the module in ImportPSModule but neither worked</li>
<li>Tried to check if Get-Item Env:\PSModulePath showed the same paths in each project and this appears to be the case</li>
<li>Check the project properties for each project in VS and made sure all the settings where the same</li>
</ul>

<p>I am running out of ideas on how to get to the bottom of this problem. Code is below</p>

<pre><code>            // Create Initial Session State for runspace.
            InitialSessionState initialSession = InitialSessionState.CreateDefault();
            initialSession.ImportPSModule(new[] { "MSOnline" });

            // Create credential object.
            PSCredential credential = new PSCredential("james@myestatehub.com", securePass);
            // Create command to connect office 365.
            Command connectCommand = new Command("Connect-MsolService");
            connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));

            // Create command to get office 365 Contacts.
            Command getUserCommand = new Command("Get-MsolContact");

            using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
            {
                // Open runspace.
                psRunSpace.Open();

                //Iterate through each command and executes it.
                foreach (var com in new Command[] { connectCommand, getUserCommand })
                    {
                    var pipe = psRunSpace.CreatePipeline();
                    pipe.Commands.Add(com);
                    // Execute command and generate results and errors (if any).
                    Collection&lt;PSObject&gt; results = pipe.Invoke(); //ERROR GET RAISED HERE
</code></pre>