0

I want to be clear about one thing that why we are using Runspace while running the PowerShell scripts in C#. It runs even though the absence of Runspace.

If it can be run without the Runspace why we need?

For eg:

PowerShell ps = PowerShell.Create();
ps.AddScript("Import-Module AzureAD");

This will work even though the absence of Runspace. So Why is it?

SaravananKS
  • 577
  • 4
  • 18

1 Answers1

0

Runspace provides APIs for creating pipelines, access session state etc.

When using PowerShell, we need to use System.Management.Automation.PowerShell, it is using System.Management.Automation.Runspaces.

using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;

namespace System.Management.Automation
{
    public sealed class PowerShell : IDisposable
    {
        ......
    }
}
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • By default, which Runspace session will be created? – SaravananKS Jul 11 '18 at 06:35
  • @SaravananKS I am not sure, but [`InitialSessionState `](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.initialsessionstate?view=powershellsdk-1.1.0) allows you to define the set of elements that should be present when Session State is created. – Joy Wang Jul 11 '18 at 06:47