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 999753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:24:13+00:00 2026-05-16T07:24:13+00:00

EDIT: I figured out how to solve the specific code broken part of my

  • 0

EDIT: I figured out how to solve the specific code broken part of my question (see my answer bellow), but I’m still looking for resources on integrating powershell and C# so please still feel free to comment or answer!

I found a simple example of making your C# objects visible to powershell scripts and I’ve been playing around with it.

Using the following code:

  public partial class MainWindow : Window
        {
            public string MyName = "Evan";
    
            public MainWindow()
            {
                InitializeComponent();
                MessageBox.Show(RunScript("$DemoForm | Get-Member"));
                MessageBox.Show(RunScript("$DemoForm.MyName"));
                MessageBox.Show(RunScript("$DemoForm.Title"));
            }
    
            private string RunScript(string scriptText)
            {
                // create Powershell runspace
    
                Runspace runspace = RunspaceFactory.CreateRunspace();
    
                // open it
    
                runspace.Open();
                runspace.SessionStateProxy.SetVariable("DemoForm", this);
                // create a pipeline and feed it the script text
    
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(scriptText);
    
                // add an extra command to transform the script
                // output objects into nicely formatted strings
    
                // remove this line to get the actual objects
                // that the script returns. For example, the script
    
                // "Get-Process" returns a collection
                // of System.Diagnostics.Process instances.
    
                pipeline.Commands.Add("Out-String");
    
                // execute the script
    
                Collection<PSObject> results = pipeline.Invoke();
    
                // close the runspace
    
                runspace.Close();
    
                // convert the script result into a single string
    
                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject obj in results)
                {
                    stringBuilder.AppendLine(obj.ToString());
                }
    
                return stringBuilder.ToString();
            }
        

}

I get the expected results from these two lines:

MessageBox.Show(RunScript("$DemoForm | Get-Member"));
MessageBox.Show(RunScript("$DemoForm.Evan"));

But this line doesn’t work (no error, it just returns an empty string):

MessageBox.Show(RunScript("$DemoForm.Title"));

Any idea why the first two work but not the third? Does it have something to do with threading (Having to access certain gui things from an sta thread?)? It seems like similar functionality was working with WindowsForms for the poster of the sample code.

Also, besides that example I linked to and this one here I haven’t been able to find many resources online about linking c# and powershell. Ultimately I’m trying to create an application that will be scriptable via powershell – does anyone know of other good online resources or a good book that covers this?

Thanks!!!!

  • 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-05-16T07:24:14+00:00Added an answer on May 16, 2026 at 7:24 am

    I got it! (with help from this video). The above code needs this line to work

    runspace.ThreadOptions = PSThreadOptions.UseCurrentThread
    

    Which makes sense to me, I’ve always had trouble with STA threads and all that jaz :).

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

Sidebar

Related Questions

EDIT: I have figured out my main issue, but still have one concern. Everything
EDIT: I figured it out from Bergi's answer in the end. Thanks Bergi. pubPrivExample
Edit: I think I've figured out how to do the binary data part. Double
Edit 4: I was finally able to solve my own issue. See checkmark answer
EDIT SO I figured out a fix. I replaced the while statement with an
EDIT I have finally figured out the problem i have been having on my
EDIT AGAIN ... I'm just a dummy and figured it out! EDIT: So, it
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: See my answer below--> I am wanting to have a view that when
I've almost got this figured out by using previous questions, but I'm running into

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.