Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:07:10+00:00 2026-06-04T03:07:10+00:00

I need to call Powershell commands via my code and I find at least

  • 0

I need to call Powershell commands via my code and I find at least 2 different examples of doing this. I’m wondering what the differences between the methods are and why I would use one as opposed to the other.

The first (simpler?) method goes something like this:

Dim command As New PSCommand()
command.AddScript("<Powershell command here>")
Dim powershell As Management.Automation.PowerShell = powershell.Create()
powershell.Commands = command
Dim results = powershell.Invoke()

results now contains a collection of Powershell objects that can be converted to strings, for example:

MsgBox(results.Item(0).ToString())

The second method looks like this:

Dim invoker As New RunspaceInvoke
Dim command As String = "<Powershell command here>"
Dim outputObjects As Collection(Of PSObject) = invoker.Invoke(command)

And then I can iterate through the collection of returned objects and convert to string in the same way:

For Each result As PSObject In outputObjects
    Console.WriteLine(result.ToString)
Next

I also know that with either method I can pipe the command to out-string to make Powershell return strings instead of objects.

My question is, which method should I use and why? They both seem the same to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:07:11+00:00Added an answer on June 4, 2026 at 3:07 am

    Lets start understanding one by one:

    PowerShell Object: Provides methods that are used to create a pipeline of commands and invoke those commands either synchronously or asynchronously within a runspace. This class also provides access to the output streams that contain data that is generated when the commands are invoked. This class is primarily intended for host applications that programmatically use Windows PowerShell to perform tasks. The pipeline here is exclusive to host application and you can make specific to PowerShell Objects. This also guarantee that you will have access to powershell results data even when the commands are sent sync and async.

    RunspaceInvoke: Allows the execution of commands from a CLR language. It can not be use if host application needs to explicitly define a pipeline. This is going to use an existing object pipeline and add Powershell commands for you. You might have other commands in the pipeline and powershell commands will be included as well. Here you are using a CLR Pipeline object and adding more commands to it and the pipeline is not exclusive to your application.

    Finally I would say using Powershell object is the best choice to use in any application which is going to host Powershell objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to call this method, but I can't find it in any namespace.
All the examples I can find using DLLImport to call C++ code from C#
I need to call a number of different procedures within an Oracle package successively
I need to call from my code for some other program . And I
I need to call a c library from my python code. The c library
In a PowerShell script, I need to determine whether a .NET method call is
I need to call a stored procedure and pass arguments in from Powershell. I
When do I actually need call this method Runtime.getRuntime().addShutdownHook() and when or why I
If I need call this functions one after other, $('#art1').animate({'width':'1000px'},1000); $('#art2').animate({'width':'1000px'},1000); $('#art3').animate({'width':'1000px'},1000); I know
I need call a DLL file in my delphi code, here is the code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.