skb
skb

Reputation: 31164

Console.WriteLine from with the Nuget Package Manager Console

If I type the following code into a typical PS window, I am able to correctly get "asdf" in the output:

[System.Console]::WriteLine("asdf")

But if I do it in the NuGet Package Manager Console nothing is output. Can anyone tell me why?

Upvotes: 1

Views: 2578

Answers (1)

Keith Hill
Keith Hill

Reputation: 202092

Because NPM is not a console app. It is hosted inside Visual Studio and implements the PowerShell host interfaces to allow the PowerShell engine to display output to what is likely a WPF window.

To get output in NPM use:

Write-Host "asdf"

or simply

"asdf"

If not in a cmdlet, you can do something like this:

private void WriteHost(string message)
{
    var runspace = Runspace.DefaultRunspace;
    var pipeline = runspace.CreatePipeline("Write-Host '" + message + "'", false);
    pipeline.Invoke();
}

Upvotes: 2

Related Questions