C# Tutorial - Run PowerShell Command from C# Winform App

Поділитися
Вставка
  • Опубліковано 15 вер 2024

КОМЕНТАРІ • 55

  • @slipsilversliver
    @slipsilversliver Рік тому +3

    Ok I'm a little new to this but if you're following this tutorial present day you'll need only one package installed:
    Microsoft.PowerShell.SDK
    Think they wrapped everything up in one box. The DLL package in the tutorial isn't available anymore.

  • @tadofr
    @tadofr 2 роки тому +3

    for anyone looking for the nuget package, open package manager and paste in this:
    Install-Package System.Management.Automation.dll -Version 10.0.10586
    hope this helps :D

    • @davhello2786
      @davhello2786 Рік тому

      thank you so much!
      i don't know why it is not still listed but with your help it worked

  • @corgi1g
    @corgi1g 4 роки тому +12

    }
    private string RunScript(string script)
    {
    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(script);
    pipeline.Commands.Add("Out-String");
    Collection results = pipeline.Invoke();
    runspace.Close();
    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject pSObject in results)
    stringBuilder.AppendLine(pSObject.ToString());
    return string.ToString();
    }

    • @crushkeyncrude5524
      @crushkeyncrude5524 Рік тому

      return string.ToString(); should be return stringBuilder.ToString();
      Can't convert a string to a string. :)

  • @manjeetnandal1
    @manjeetnandal1 5 років тому +1

    Many useful!
    Great thanks!
    You are like GOD for us who are self-starter.

    • @foxlearn
      @foxlearn  5 років тому

      You're welcome. Thank you !

  • @GurnBograt1986
    @GurnBograt1986 Рік тому

    Very useful tutorial. Thanks.

  • @aledario18
    @aledario18 2 роки тому +1

    I cannot find System.Management.Automation.dll, I find System.Management.Automation, is the same Library?

    • @seichaibao
      @seichaibao 2 роки тому +2

      Yea, I'm looking for this library too. Cannot find it in Nuget Package Manager. Perhaps you can try another library eg: Cake.Powershell. It does have the Runspace and RunspaceFactory features.

  • @kjellnoekker4026
    @kjellnoekker4026 4 роки тому

    Thank you very much- this is a very useful tutorial!

  • @buseacun6788
    @buseacun6788 2 роки тому

    Thank you for the video. It is very helpfull for me. I am trying to use ffmpeg in winforms with using powershell like this so could you make a video for this kind of tutorial?

  • @superJK92
    @superJK92 Рік тому

    Thank you I used this to make a prompt that runs when my iGPU temp is over 200 degrees F that will ask if I want to reboot my pc

  • @jtg4416
    @jtg4416 2 роки тому +1

    i cant find the nuget package

  • @boessi
    @boessi 4 роки тому +3

    Hello, very nice video! Unfortunately, the Nuget Packet Manager cannot find the "System.Management.Automation.dll". What could be the reason for that?

    • @foxlearn
      @foxlearn  4 роки тому

      Please try to find again. i think you find name wrong

    • @a20axf
      @a20axf 4 роки тому

      @@foxlearn No, it has been removed due to age. I found it on the Nuget page stating that so it is no longer in use.

    • @anthonydiamond906
      @anthonydiamond906 4 роки тому +5

      Can add a reference. That DLL is located at C:\Program Files(x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll

    • @a20axf
      @a20axf 4 роки тому

      Anthony Diamond cheers for that, I’m probably missing something else quite obvious. Relatively new to C# but, making powershell scripts work from it will make life a lot easier 👌🏻

    • @hesapsiz
      @hesapsiz 3 роки тому

      @@anthonydiamond906 yes you can Add it manually browsing that directory. Than you can use the " using System.Management.Automation.Runspaces;" reference. Everything is fine ist very interesting.

  • @spankymasterc
    @spankymasterc 5 років тому +2

    How do I make this run an existing powershell script? Instead of having to type something into the box i would like a button that runs the powershell script and also outputs the results

    • @foxlearn
      @foxlearn  5 років тому

      OK. Thank you for your suggestion. I'll upload soon !

    • @michael_azzer
      @michael_azzer 4 роки тому +1

      you can use this code
      string script= File.ReadAllText("D:\\s.txt");
      Runspace runspace = RunspaceFactory.CreateRunspace();
      runspace.Open();
      Pipeline pipeline = runspace.CreatePipeline();
      pipeline.Commands.AddScript(script);
      pipeline.Commands.Add("Out-String");
      Collection result = pipeline.Invoke();
      runspace.Close();
      StringBuilder stringBuilder = new StringBuilder();
      foreach (PSObject pSObject in result)
      {
      stringBuilder.AppendLine(pSObject.ToString());
      }
      return stringBuilder.ToString();

  • @Banshee12x
    @Banshee12x 4 роки тому

    It is possible to control remote mahines over network - run remote powershell scripts via network with interface in windows forms application?
    What do You think?

  • @vcnotes8819
    @vcnotes8819 2 роки тому

    can you please help me with "How to create button which can execute complex and mulit-line scripts from single button? Any help will be of great honor!

  • @skamlo6387
    @skamlo6387 2 роки тому

    Is there an alternative to .NET 5.0 because currently System.Management.Automation supports only .NET 6.0 or later?

  • @diego-vw2in
    @diego-vw2in 3 роки тому

    And how if my script starts a process, like as:
    Start-Process -FilePath "SteamCMD\steamcmd.exe" -ArgumentList \"+login login pass +force_install_dir C:\Dir +app_update 920720 validate +quit" ?
    It opens a new window instead output to the textbox.
    ty!!!!

  • @Conversion108
    @Conversion108 Рік тому

    how can i get this to work with commands like mkdir for me it just crashes when running commands like this

  • @nuthanmurari
    @nuthanmurari 3 роки тому

    I want to execute mongorestore command using command prompt through c# console App. Here the requirement is cmd prompt should run backend, Cmd prompt map to the specified path and finally run our mongorestore command. Is it possible if yes please do a video on this

  • @dannomanno1358
    @dannomanno1358 Рік тому +1

    If you need it to work with .NET Framework then you'll want the nuget package: Microsoft.PowerShell.5.ReferenceAssemblies

    • @chrisk5404
      @chrisk5404 Рік тому

      That is what i was looking for, thx man :)

  • @EssamTalks
    @EssamTalks 2 роки тому

    How can i write a full powershell code into my c# code?

  • @ThatSameer
    @ThatSameer 4 роки тому

    Hi Friend. I need to import a module from a psd1 file, and then add the script to run on the backend, instead of user input. in c#. How can i do this please?

  • @cartec2004
    @cartec2004 4 роки тому

    c++ console application to show disk usage based on unix command .. can help me please thnaks

  • @alzubairit
    @alzubairit 5 років тому +1

    Hi ,
    Please make a video to make a remote control pc in the same server like anydisck ...

    • @foxlearn
      @foxlearn  5 років тому +1

      OK.Thank you for your suggestion. I'll make soon

    • @alzubairit
      @alzubairit 5 років тому

      @@foxlearn thanx pro

  • @monakhaled3841
    @monakhaled3841 5 років тому

    GOOD.

  • @alirezaabedi1885
    @alirezaabedi1885 5 років тому

    gooooooooooooooood

  • @swerytv4342
    @swerytv4342 5 років тому +1

    Good. teacher i nées to know how to make login, authorization(délecter one by tree list) and users with C# Devepress.
    Merci.

    • @foxlearn
      @foxlearn  5 років тому

      OK. Thank you for your suggestion. i'll upload on my site

    • @swerytv4342
      @swerytv4342 5 років тому

      @@foxlearn ok With of system of authorization with treeList selectable by checkbox. Thanks i'm waiting

  • @tariquesultanSyed
    @tariquesultanSyed 3 роки тому

    how to run a PowerShell script with parameters

  • @upgradeu3928
    @upgradeu3928 5 років тому +1

    Can u make tutorial for asp net core? plz)

    • @foxlearn
      @foxlearn  5 років тому

      OK. Thank you for your suggestion. I'll upload soon !

  • @kaanylmaz1008
    @kaanylmaz1008 4 роки тому

    Can we also use cmd prompt?

    • @foxlearn
      @foxlearn  3 роки тому

      I think no, you should use powershell tool

  • @mandarjoshi7318
    @mandarjoshi7318 3 роки тому

    Ow can we get write-host statement in c#

    • @foxlearn
      @foxlearn  3 роки тому

      Let me try it. Thank you for your suggestion !

  • @tuleviku
    @tuleviku 4 роки тому

    a little useless when you cant use pipes or spaces in command...

    • @LemmiwinksH
      @LemmiwinksH 3 роки тому

      Very disappointing..
      I've been trying to find a way to simplify setting up Microsoft Teams VoIP Users via a GUI with a background PowerShell process, and I thought this tutorial would do it. Unfortunately the "connect-microsoftteams" command can't be run via this method, as the microsoftteams module is not loadable...
      If anyone has found a solution around this, I'd be appreciative for sure.