1
        public void execute_ps_script()
        {                      
            PowerShell ps = PowerShell.Create();
            
            ps.AddScript(" Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ | Format-Table -Property Title, pubDate");

            {
                Collection<PSObject> results = ps.Invoke();
                foreach (var result in results)
                {
                    Console.WriteLine(result);
                }               
            }            
        }

If I run the above code with breakpoint at console.WriteLine():

Variable Value
result {Microsoft.PowerShell.Commands.Internal.Format.FormatStartData}
results Count = 14

14 values in results are:

{Microsoft.PowerShell.Commands.Internal.Format.FormatStartData}

The output for Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ | Format-Table -Property Title, pubDate in PowerShell is:

title                                                        pubDate
-----                                                        -------
Preview updating PowerShell 7.2 with Microsoft Update        Wed, 16 Jun 2021 16:29:05 +0000
PowerShell for Visual Studio Code May 2021 Update            Wed, 02 Jun 2021 23:18:17 +0000
SecretManagement Module v1.1.0 preview update                Tue, 25 May 2021 18:30:36 +0000
Announcing PlatyPS 2.0.0-Preview1                            Thu, 20 May 2021 19:08:32 +0000
Announcing PSDesiredStateConfiguration on PowerShell Gallery Fri, 14 May 2021 17:46:23 +0000
Announcing PowerShell Crescendo Preview.2                    Thu, 06 May 2021 15:24:25 +0000
Optimizing your $Profile                                     Tue, 06 Apr 2021 18:55:09 +0000
Announcing the preview of PSArm                              Wed, 31 Mar 2021 17:02:54 +0000
SecretManagement and SecretStore are Generally Available     Thu, 25 Mar 2021 18:22:07 +0000
SecretStore Release Candidate 3                              Mon, 15 Mar 2021 22:12:04 +0000

How do I get the same output in Visual Studio when running PowerShell command using C#?

hellouniverse
  • 113
  • 1
  • 12

1 Answers1

1

This resolved the issue:

        public void execute_ps_script()
        {                      
            PowerShell ps = PowerShell.Create();
            
            ps.AddScript(" Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ | Format-Table -Property Title, pubDate | Out-String");

            {
                Collection<PSObject> results = ps.Invoke();
                foreach (var result in results)
                {
                    Console.WriteLine(result);
                }               
            }            
        }

Added Out-String in the script.

hellouniverse
  • 113
  • 1
  • 12
  • 3
    Yes,`Out-String` renders a _string_ representation of the _formatting objects_ that the `Format-*` cmdlets output (which in the console are interpreted by PowerShell itself). While using `Out-String` works to get the _for-display_ representation of objects, the usual caveat applies: The sole purpose of the `Format-*` cmdlet is to provide _formatting instructions_ to PowerShell's output-formatting system - see [this answer](https://stackoverflow.com/a/55174715/45375). In short: only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_. – mklement0 Jul 18 '21 at 22:08
  • I am glad to hear that your problem has been solved, you can click '✔' to mark your reply as an answer. It will also help others to solve the similar issue. – Jack J Jun Jul 21 '21 at 05:40