title: How to get the result of a Powershell cmdlet in C# unit method
body: <p>I have below PowerShell cmnd which runs fine in PowerShell and I am also able to call and execute it from a unit test in C# as described below. But my problem is in PowerShell window I write $rec.Categories.Count and then I get result. How can I write this in PowerShell script which I call in C# and get the value in there to test?</p>
  <p>In PowerShell window:</p>
  <pre><code>$inv = Get-Inventory -Project 'TestDemo'
  $inv.Categories.Count
  4
  </code></pre>
  <p>Same script I have in C# and I am calling it by below method and it runs successfully but I don't know the way to get &quot;4&quot; in my result variable.</p>
  <pre><code>[Fact]
          public void VerifyGetInventoriesOfaProject()
          {
              string path = Path.Combine( Root, @&quot;TestData\GetInventory.ps1&quot; );
              Runspace runspace = RunspaceFactory.CreateRunspace();
              runspace.Open();
              Pipeline pipeline = runspace.CreatePipeline();
              pipeline.Commands.AddScript( path );
              var results = pipeline.Invoke();
              runspace.Close();
             // Assert.Equal( 5, results );
  }
  </code></pre>