Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Run PowerShell Script(.ps1) From Qt Project
Forum Update on Monday, May 27th 2025

How to Run PowerShell Script(.ps1) From Qt Project

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    R_Irudezu
    wrote on 24 Sept 2018, 21:05 last edited by
    #1

    I want to run a simple .ps1 script, but i couldn't do it. I tried to use system command like that:

    system("powershell C:/Users/doe/Desktop/Demo.ps1");
    

    I tried to use QProcess like that:

        QProcess process;
        process.setWorkingDirectory("C:/Users/doe/Desktop");
        QString cmd = ("Demo.ps1");
        process.startDetached(cmd);
    

    need help about that :/

    Keizoku wa chikaranari.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 24 Sept 2018, 21:15 last edited by
      #2

      Hi,

      Shouldn't it be something like:

      QProcess process;
          process.setWorkingDirectory("C:/Users/doe/Desktop");
          QString cmd("powershell");
          QStringList parameters{"Demo.ps1"};
          process.start(cmd, parameters);
      

      By the way, the version of startDetached you called is static so you're not using as you should and you won't get the result you expect.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply 25 Sept 2018, 04:00
      6
      • S SGaist
        24 Sept 2018, 21:15

        Hi,

        Shouldn't it be something like:

        QProcess process;
            process.setWorkingDirectory("C:/Users/doe/Desktop");
            QString cmd("powershell");
            QStringList parameters{"Demo.ps1"};
            process.start(cmd, parameters);
        

        By the way, the version of startDetached you called is static so you're not using as you should and you won't get the result you expect.

        R Offline
        R Offline
        R_Irudezu
        wrote on 25 Sept 2018, 04:00 last edited by
        #3

        @SGaist Hi, i'm already asking how can i do it correctly. Any other method is okay. I'm expecting an answer to how to do it...

        Keizoku wa chikaranari.

        J J 2 Replies Last reply 25 Sept 2018, 04:43
        0
        • R R_Irudezu
          25 Sept 2018, 04:00

          @SGaist Hi, i'm already asking how can i do it correctly. Any other method is okay. I'm expecting an answer to how to do it...

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 25 Sept 2018, 04:43 last edited by jsulm
          #4

          @R_Irudezu From documentation (http://doc.qt.io/qt-5/qprocess.html#startDetached):
          "Starts the program set by setProgram() with arguments set by setArguments() in a new process, and detaches from it."
          So, you need to set program to execute (powershell) using setProgram() and parameters (C:/Users/doe/Desktop) using setArguments() and then call startDetached() (without parameters!).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • R R_Irudezu
            25 Sept 2018, 04:00

            @SGaist Hi, i'm already asking how can i do it correctly. Any other method is okay. I'm expecting an answer to how to do it...

            J Online
            J Online
            JonB
            wrote on 25 Sept 2018, 08:00 last edited by
            #5

            @R_Irudezu What @SGaist has posted above is the correct way to do it.

            J 1 Reply Last reply 25 Sept 2018, 08:01
            1
            • J JonB
              25 Sept 2018, 08:00

              @R_Irudezu What @SGaist has posted above is the correct way to do it.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 25 Sept 2018, 08:01 last edited by
              #6

              @JonB I guess he wants to use startDetached

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply 25 Sept 2018, 08:07
              0
              • J jsulm
                25 Sept 2018, 08:01

                @JonB I guess he wants to use startDetached

                J Online
                J Online
                JonB
                wrote on 25 Sept 2018, 08:07 last edited by
                #7

                @jsulm
                Why? I'm not a Powershell user, but I see people running Powershell commands interactively all the time. Sometimes people here say they (think they) want startDetached(), but it turns out they don't!

                Assuming you are correct and he does, then he should follow your advice. Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is static, unlike QProcess::start().

                J 1 Reply Last reply 25 Sept 2018, 08:08
                0
                • J JonB
                  25 Sept 2018, 08:07

                  @jsulm
                  Why? I'm not a Powershell user, but I see people running Powershell commands interactively all the time. Sometimes people here say they (think they) want startDetached(), but it turns out they don't!

                  Assuming you are correct and he does, then he should follow your advice. Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is static, unlike QProcess::start().

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 25 Sept 2018, 08:08 last edited by
                  #8

                  @JonB said in How to Run PowerShell Script(.ps1) From Qt Project:

                  Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is static

                  That's why I pointed to http://doc.qt.io/qt-5/qprocess.html#startDetached :-)

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply 25 Sept 2018, 08:12
                  0
                  • J jsulm
                    25 Sept 2018, 08:08

                    @JonB said in How to Run PowerShell Script(.ps1) From Qt Project:

                    Bear in mind that http://doc.qt.io/qt-5/qprocess.html#startDetached-1 is static

                    That's why I pointed to http://doc.qt.io/qt-5/qprocess.html#startDetached :-)

                    J Online
                    J Online
                    JonB
                    wrote on 25 Sept 2018, 08:12 last edited by JonB
                    #9

                    @jsulm
                    Indeed you did, and I have up-voted your answer!

                    I was drawing OP's attention/stressing to fact of static, in hopeful anticipation of not having to deal with incorrect code as per https://forum.qt.io/topic/94876/qiodevice-read-qprocess-device-not-open/2 which I have been answering :)

                    1 Reply Last reply
                    2
                    • R Offline
                      R Offline
                      R_Irudezu
                      wrote on 25 Sept 2018, 13:11 last edited by R_Irudezu
                      #10

                      I tried every suggestions that you said gentlemen. But nothing worked.
                      The script file (test.ps1) works when i execute it in the powershell window as .\test.ps1.

                      But i want to do it in my C++ Qt program. Both system() and QProcess[start ort startDetach] are not working. I set code into a button slot function (clicked), a powershell window opening for ~0.2 second and that's it, test.ps1 wasn't executed.

                      Really need help about this :/...

                      @R_Irudezu @jsulm @JonB @SGaist

                      Keizoku wa chikaranari.

                      R 1 Reply Last reply 25 Sept 2018, 14:55
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 25 Sept 2018, 13:15 last edited by
                        #11

                        Did you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        A 1 Reply Last reply 25 Sept 2018, 13:59
                        2
                        • S SGaist
                          25 Sept 2018, 13:15

                          Did you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?

                          A Offline
                          A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on 25 Sept 2018, 13:59 last edited by
                          #12

                          @R_Irudezu: And to add to @SGaist,

                          are you sure the windows powershell can find your script? In which directory is it located? Please note that the program is usually build in an other directory than the program sources reside.

                          Regards

                          Qt has to stay free or it will die.

                          1 Reply Last reply
                          1
                          • R R_Irudezu
                            25 Sept 2018, 13:11

                            I tried every suggestions that you said gentlemen. But nothing worked.
                            The script file (test.ps1) works when i execute it in the powershell window as .\test.ps1.

                            But i want to do it in my C++ Qt program. Both system() and QProcess[start ort startDetach] are not working. I set code into a button slot function (clicked), a powershell window opening for ~0.2 second and that's it, test.ps1 wasn't executed.

                            Really need help about this :/...

                            @R_Irudezu @jsulm @JonB @SGaist

                            R Offline
                            R Offline
                            R_Irudezu
                            wrote on 25 Sept 2018, 14:55 last edited by
                            #13

                            Yes, it finds. I tested with creating directory. It created under build of project file. So i created my powershell.script under build directory.

                            Keizoku wa chikaranari.

                            J 1 Reply Last reply 25 Sept 2018, 15:06
                            0
                            • R R_Irudezu
                              25 Sept 2018, 14:55

                              Yes, it finds. I tested with creating directory. It created under build of project file. So i created my powershell.script under build directory.

                              J Online
                              J Online
                              JonB
                              wrote on 25 Sept 2018, 15:06 last edited by
                              #14

                              @R_Irudezu
                              You really need to start by heeding @SGaist's post above

                              Did you check the exitCode ? Read the standard error ? Read the standard output ? Used errorOccured ?

                              You need to do follow all of the links he supplied and act on them in your code if you expect to discover what is going on.

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                R_Irudezu
                                wrote on 27 Sept 2018, 16:35 last edited by R_Irudezu
                                #15

                                as @SGaist said, i checked exitCode and Errors. I solved my problem, run an .exe with a IP string parameter. Running powershell script required some permissions.

                                    QString executer = "C:/Users/doe/Desktop/IP_Test/ip_test.exe";
                                    QStringList params;
                                    params << "192.168.1.31";  
                                    externalProcess = new QProcess();
                                    externalProcess ->setWorkingDirectory("C:/Users/doe/Desktop/frames");
                                    connect(externalProcess , SIGNAL(finished(int)), this, SLOT(externalProcess (int)));
                                
                                    connect(externalProcess , SIGNAL(stateChanged(QProcess::ProcessState)), this, 
                                    SLOT(aynxProcessState(QProcess::ProcessState)));
                                
                                    externalProcess ->start(executer, params);
                                    externalProcess ->waitForFinished(-1); //wait until .exe job finished
                                    int exitCode = externalProcess->exitCode();
                                

                                Keizoku wa chikaranari.

                                J 1 Reply Last reply 27 Sept 2018, 18:53
                                0
                                • R R_Irudezu
                                  27 Sept 2018, 16:35

                                  as @SGaist said, i checked exitCode and Errors. I solved my problem, run an .exe with a IP string parameter. Running powershell script required some permissions.

                                      QString executer = "C:/Users/doe/Desktop/IP_Test/ip_test.exe";
                                      QStringList params;
                                      params << "192.168.1.31";  
                                      externalProcess = new QProcess();
                                      externalProcess ->setWorkingDirectory("C:/Users/doe/Desktop/frames");
                                      connect(externalProcess , SIGNAL(finished(int)), this, SLOT(externalProcess (int)));
                                  
                                      connect(externalProcess , SIGNAL(stateChanged(QProcess::ProcessState)), this, 
                                      SLOT(aynxProcessState(QProcess::ProcessState)));
                                  
                                      externalProcess ->start(executer, params);
                                      externalProcess ->waitForFinished(-1); //wait until .exe job finished
                                      int exitCode = externalProcess->exitCode();
                                  
                                  J Online
                                  J Online
                                  JonB
                                  wrote on 27 Sept 2018, 18:53 last edited by
                                  #16

                                  @R_Irudezu
                                  Good stuff, well done!

                                  Move that exitCode() line down from

                                      externalProcess ->exitCode();
                                      externalProcess ->waitForFinished(-1); //wait until .exe job finished
                                  

                                  to

                                      externalProcess ->waitForFinished(-1); //wait until .exe job finished
                                      int exitCode = externalProcess ->exitCode();
                                  

                                  at least for anyone future reading this :)

                                  R 1 Reply Last reply 27 Sept 2018, 19:52
                                  1
                                  • J JonB
                                    27 Sept 2018, 18:53

                                    @R_Irudezu
                                    Good stuff, well done!

                                    Move that exitCode() line down from

                                        externalProcess ->exitCode();
                                        externalProcess ->waitForFinished(-1); //wait until .exe job finished
                                    

                                    to

                                        externalProcess ->waitForFinished(-1); //wait until .exe job finished
                                        int exitCode = externalProcess ->exitCode();
                                    

                                    at least for anyone future reading this :)

                                    R Offline
                                    R Offline
                                    R_Irudezu
                                    wrote on 27 Sept 2018, 19:52 last edited by
                                    #17

                                    @JonB i was read exit code with a function but yes, for future reading i will edit post as you said. Thanks :)

                                    Keizoku wa chikaranari.

                                    1 Reply Last reply
                                    0

                                    9/17

                                    25 Sept 2018, 08:12

                                    • Login

                                    • Login or register to search.
                                    9 out of 17
                                    • First post
                                      9/17
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved