Question: I am trying to call powershell file from c# code asynchronously like fire and forget. I tried using Powershell.BeginInvoke but it does not invokes the file until I call EndInvoke. Anyway, I tried pipeline.InvokeAsync method but still am not able to invoke the script.It works when I just call Invoke method. Below is the code I tried

```
using(  PowerShell shell = PowerShell.Create())
        { 
Pipeline pipe = shell.Runspace.CreatePipeline();
            pipe.Commands.AddScript("d:\\AddContentTest.ps1"); 
pipe.InvokeAsync(); 
}

```

Any help is much appreciated...............

Answer: 
You are calling `InvokeAsync` without `await`ing it. The method returns immediately, and the PowerShell will dispose since you're exiting the `using` scope.

Try this:

```
public async Task InvokePowerShellAsync()
{ 
    using (PowerShell shell = PowerShell.Create())
    { 
         Pipeline pipe = shell.Runspace.CreatePipeline();
         pipe.Commands.AddScript("d:\\AddContentTest.ps1"); 

         await pipe.InvokeAsync(); 
    }
}

```

**Edit**

To complete the answer, and to your comment, you can use a background thread although I dont recommend that approach:

```
public void InvokePowerShellAsync()
{ 
    Task.Run(() => 
    {
        using (PowerShell shell = PowerShell.Create())
        { 
            Pipeline pipe = shell.Runspace.CreatePipeline();
            pipe.Commands.AddScript("d:\\AddContentTest.ps1");

            pipe.Invoke();
        }        
    }      
}

```
this is an unmanaged port. Awaiting the [Pipeline.InvokeAsync](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.pipeline.invokeasync?view=powershellsdk-7.0.0) will not work.

MS documentation will do better than me:

> 
> 1. Results are returned through the Output reader. 2) When pipeline is
> invoked using InvokeAsync, **invocation doesn't finish until Input to
> pipeline is closed**. Caller of InvokeAsync must close the input pipe
> after all input has been written to input pipe. Input pipe is closed
> by calling Pipeline.Input.Close();
> 
> 
> 

Instead, you need to call the **Pipeline.Input.Close()** prior to and *must not await* the **InvokeAsync**