Powershell Script to run exe file with parameters












1















I need script to run exe file with parameters.
That's what I wrote, if there's a better way to do it?






$Command = "\NetworkpathRestart.exe"
$Parms = "/t:21600 /m:360 /r /f"
$Prms = $Parms.Split(" ")
& "$Command" $Prms





thanks










share|improve this question























  • I would use Start-Process but your example works as well.

    – TobyU
    Nov 14 '18 at 15:09






  • 1





    You don't need " around $Command.

    – Bill_Stewart
    Nov 14 '18 at 16:47






  • 1





    @Bill_Stewart "$($Command.ToString())" :P

    – TheIncorrigible1
    Nov 14 '18 at 17:33











  • @TheIncorrigible1 :-)

    – Bill_Stewart
    Nov 14 '18 at 18:09
















1















I need script to run exe file with parameters.
That's what I wrote, if there's a better way to do it?






$Command = "\NetworkpathRestart.exe"
$Parms = "/t:21600 /m:360 /r /f"
$Prms = $Parms.Split(" ")
& "$Command" $Prms





thanks










share|improve this question























  • I would use Start-Process but your example works as well.

    – TobyU
    Nov 14 '18 at 15:09






  • 1





    You don't need " around $Command.

    – Bill_Stewart
    Nov 14 '18 at 16:47






  • 1





    @Bill_Stewart "$($Command.ToString())" :P

    – TheIncorrigible1
    Nov 14 '18 at 17:33











  • @TheIncorrigible1 :-)

    – Bill_Stewart
    Nov 14 '18 at 18:09














1












1








1








I need script to run exe file with parameters.
That's what I wrote, if there's a better way to do it?






$Command = "\NetworkpathRestart.exe"
$Parms = "/t:21600 /m:360 /r /f"
$Prms = $Parms.Split(" ")
& "$Command" $Prms





thanks










share|improve this question














I need script to run exe file with parameters.
That's what I wrote, if there's a better way to do it?






$Command = "\NetworkpathRestart.exe"
$Parms = "/t:21600 /m:360 /r /f"
$Prms = $Parms.Split(" ")
& "$Command" $Prms





thanks






$Command = "\NetworkpathRestart.exe"
$Parms = "/t:21600 /m:360 /r /f"
$Prms = $Parms.Split(" ")
& "$Command" $Prms





$Command = "\NetworkpathRestart.exe"
$Parms = "/t:21600 /m:360 /r /f"
$Prms = $Parms.Split(" ")
& "$Command" $Prms






windows powershell scripting exe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 15:06









MeniMeni

132




132













  • I would use Start-Process but your example works as well.

    – TobyU
    Nov 14 '18 at 15:09






  • 1





    You don't need " around $Command.

    – Bill_Stewart
    Nov 14 '18 at 16:47






  • 1





    @Bill_Stewart "$($Command.ToString())" :P

    – TheIncorrigible1
    Nov 14 '18 at 17:33











  • @TheIncorrigible1 :-)

    – Bill_Stewart
    Nov 14 '18 at 18:09



















  • I would use Start-Process but your example works as well.

    – TobyU
    Nov 14 '18 at 15:09






  • 1





    You don't need " around $Command.

    – Bill_Stewart
    Nov 14 '18 at 16:47






  • 1





    @Bill_Stewart "$($Command.ToString())" :P

    – TheIncorrigible1
    Nov 14 '18 at 17:33











  • @TheIncorrigible1 :-)

    – Bill_Stewart
    Nov 14 '18 at 18:09

















I would use Start-Process but your example works as well.

– TobyU
Nov 14 '18 at 15:09





I would use Start-Process but your example works as well.

– TobyU
Nov 14 '18 at 15:09




1




1





You don't need " around $Command.

– Bill_Stewart
Nov 14 '18 at 16:47





You don't need " around $Command.

– Bill_Stewart
Nov 14 '18 at 16:47




1




1





@Bill_Stewart "$($Command.ToString())" :P

– TheIncorrigible1
Nov 14 '18 at 17:33





@Bill_Stewart "$($Command.ToString())" :P

– TheIncorrigible1
Nov 14 '18 at 17:33













@TheIncorrigible1 :-)

– Bill_Stewart
Nov 14 '18 at 18:09





@TheIncorrigible1 :-)

– Bill_Stewart
Nov 14 '18 at 18:09












1 Answer
1






active

oldest

votes


















3














You have a couple options when running an external executable.





Splatting



$command = '\netpathrestart.exe'
$params = '/t:21600', '/m:360', '/r', '/f'
& $command @params


This method will essentially join your array as arguments to the executable. This allows your list of arguments to be cleaner and can be re-written as:



$params = @(
'/t:21600'
'/m:360'
'/r'
'/f'
)


This is usually my favorite way to address the problem.





Call the executable with arguments at once



You don't necessarily need to have variables or even the call operator (&) if you don't have spaces in arguments, path, etc.



\netpathrestart.exe /t:21600 /m:360 /r /f




Start-Process



This is my second go-to because it gives me more control over the eventual process. Sometimes executables spawn sub-processes and your call operator won't wait for the process to end before moving on in your script. This method gives you control over that.



$startParams = @{
'FilePath' = '\netpathrestart.exe'
'ArgumentList' = '/t:21600', '/m:360', '/r', '/f'
'Wait' = $true
'PassThru' = $true
}
$proc = Start-Process @startParams
$proc.ExitCode




System.Diagnostics.Process



Last of the methods I know, using the Process .NET class directly. I use this method if I need even more control of the process, such as collecting its output:



try
{
$proc = [System.Diagnostics.Process]::Start([System.Diagnostics.ProcessStartInfo]@{
'FileName' = "\netsharerestart.exe"
'Arguments' = '/t:21600 /m:360 /r /f'
'CreateNoWindow' = $true
'UseShellExecute' = $false
'RedirectStandardOutput' = $true
})
$output = $proc.StandardOutput
$output.ReadToEnd()
}
finally
{
if ($null -ne $proc)
{
$proc.Dispose()
}
if ($null -ne $output)
{
$output.Dispose()
}
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303222%2fpowershell-script-to-run-exe-file-with-parameters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You have a couple options when running an external executable.





    Splatting



    $command = '\netpathrestart.exe'
    $params = '/t:21600', '/m:360', '/r', '/f'
    & $command @params


    This method will essentially join your array as arguments to the executable. This allows your list of arguments to be cleaner and can be re-written as:



    $params = @(
    '/t:21600'
    '/m:360'
    '/r'
    '/f'
    )


    This is usually my favorite way to address the problem.





    Call the executable with arguments at once



    You don't necessarily need to have variables or even the call operator (&) if you don't have spaces in arguments, path, etc.



    \netpathrestart.exe /t:21600 /m:360 /r /f




    Start-Process



    This is my second go-to because it gives me more control over the eventual process. Sometimes executables spawn sub-processes and your call operator won't wait for the process to end before moving on in your script. This method gives you control over that.



    $startParams = @{
    'FilePath' = '\netpathrestart.exe'
    'ArgumentList' = '/t:21600', '/m:360', '/r', '/f'
    'Wait' = $true
    'PassThru' = $true
    }
    $proc = Start-Process @startParams
    $proc.ExitCode




    System.Diagnostics.Process



    Last of the methods I know, using the Process .NET class directly. I use this method if I need even more control of the process, such as collecting its output:



    try
    {
    $proc = [System.Diagnostics.Process]::Start([System.Diagnostics.ProcessStartInfo]@{
    'FileName' = "\netsharerestart.exe"
    'Arguments' = '/t:21600 /m:360 /r /f'
    'CreateNoWindow' = $true
    'UseShellExecute' = $false
    'RedirectStandardOutput' = $true
    })
    $output = $proc.StandardOutput
    $output.ReadToEnd()
    }
    finally
    {
    if ($null -ne $proc)
    {
    $proc.Dispose()
    }
    if ($null -ne $output)
    {
    $output.Dispose()
    }
    }





    share|improve this answer






























      3














      You have a couple options when running an external executable.





      Splatting



      $command = '\netpathrestart.exe'
      $params = '/t:21600', '/m:360', '/r', '/f'
      & $command @params


      This method will essentially join your array as arguments to the executable. This allows your list of arguments to be cleaner and can be re-written as:



      $params = @(
      '/t:21600'
      '/m:360'
      '/r'
      '/f'
      )


      This is usually my favorite way to address the problem.





      Call the executable with arguments at once



      You don't necessarily need to have variables or even the call operator (&) if you don't have spaces in arguments, path, etc.



      \netpathrestart.exe /t:21600 /m:360 /r /f




      Start-Process



      This is my second go-to because it gives me more control over the eventual process. Sometimes executables spawn sub-processes and your call operator won't wait for the process to end before moving on in your script. This method gives you control over that.



      $startParams = @{
      'FilePath' = '\netpathrestart.exe'
      'ArgumentList' = '/t:21600', '/m:360', '/r', '/f'
      'Wait' = $true
      'PassThru' = $true
      }
      $proc = Start-Process @startParams
      $proc.ExitCode




      System.Diagnostics.Process



      Last of the methods I know, using the Process .NET class directly. I use this method if I need even more control of the process, such as collecting its output:



      try
      {
      $proc = [System.Diagnostics.Process]::Start([System.Diagnostics.ProcessStartInfo]@{
      'FileName' = "\netsharerestart.exe"
      'Arguments' = '/t:21600 /m:360 /r /f'
      'CreateNoWindow' = $true
      'UseShellExecute' = $false
      'RedirectStandardOutput' = $true
      })
      $output = $proc.StandardOutput
      $output.ReadToEnd()
      }
      finally
      {
      if ($null -ne $proc)
      {
      $proc.Dispose()
      }
      if ($null -ne $output)
      {
      $output.Dispose()
      }
      }





      share|improve this answer




























        3












        3








        3







        You have a couple options when running an external executable.





        Splatting



        $command = '\netpathrestart.exe'
        $params = '/t:21600', '/m:360', '/r', '/f'
        & $command @params


        This method will essentially join your array as arguments to the executable. This allows your list of arguments to be cleaner and can be re-written as:



        $params = @(
        '/t:21600'
        '/m:360'
        '/r'
        '/f'
        )


        This is usually my favorite way to address the problem.





        Call the executable with arguments at once



        You don't necessarily need to have variables or even the call operator (&) if you don't have spaces in arguments, path, etc.



        \netpathrestart.exe /t:21600 /m:360 /r /f




        Start-Process



        This is my second go-to because it gives me more control over the eventual process. Sometimes executables spawn sub-processes and your call operator won't wait for the process to end before moving on in your script. This method gives you control over that.



        $startParams = @{
        'FilePath' = '\netpathrestart.exe'
        'ArgumentList' = '/t:21600', '/m:360', '/r', '/f'
        'Wait' = $true
        'PassThru' = $true
        }
        $proc = Start-Process @startParams
        $proc.ExitCode




        System.Diagnostics.Process



        Last of the methods I know, using the Process .NET class directly. I use this method if I need even more control of the process, such as collecting its output:



        try
        {
        $proc = [System.Diagnostics.Process]::Start([System.Diagnostics.ProcessStartInfo]@{
        'FileName' = "\netsharerestart.exe"
        'Arguments' = '/t:21600 /m:360 /r /f'
        'CreateNoWindow' = $true
        'UseShellExecute' = $false
        'RedirectStandardOutput' = $true
        })
        $output = $proc.StandardOutput
        $output.ReadToEnd()
        }
        finally
        {
        if ($null -ne $proc)
        {
        $proc.Dispose()
        }
        if ($null -ne $output)
        {
        $output.Dispose()
        }
        }





        share|improve this answer















        You have a couple options when running an external executable.





        Splatting



        $command = '\netpathrestart.exe'
        $params = '/t:21600', '/m:360', '/r', '/f'
        & $command @params


        This method will essentially join your array as arguments to the executable. This allows your list of arguments to be cleaner and can be re-written as:



        $params = @(
        '/t:21600'
        '/m:360'
        '/r'
        '/f'
        )


        This is usually my favorite way to address the problem.





        Call the executable with arguments at once



        You don't necessarily need to have variables or even the call operator (&) if you don't have spaces in arguments, path, etc.



        \netpathrestart.exe /t:21600 /m:360 /r /f




        Start-Process



        This is my second go-to because it gives me more control over the eventual process. Sometimes executables spawn sub-processes and your call operator won't wait for the process to end before moving on in your script. This method gives you control over that.



        $startParams = @{
        'FilePath' = '\netpathrestart.exe'
        'ArgumentList' = '/t:21600', '/m:360', '/r', '/f'
        'Wait' = $true
        'PassThru' = $true
        }
        $proc = Start-Process @startParams
        $proc.ExitCode




        System.Diagnostics.Process



        Last of the methods I know, using the Process .NET class directly. I use this method if I need even more control of the process, such as collecting its output:



        try
        {
        $proc = [System.Diagnostics.Process]::Start([System.Diagnostics.ProcessStartInfo]@{
        'FileName' = "\netsharerestart.exe"
        'Arguments' = '/t:21600 /m:360 /r /f'
        'CreateNoWindow' = $true
        'UseShellExecute' = $false
        'RedirectStandardOutput' = $true
        })
        $output = $proc.StandardOutput
        $output.ReadToEnd()
        }
        finally
        {
        if ($null -ne $proc)
        {
        $proc.Dispose()
        }
        if ($null -ne $output)
        {
        $output.Dispose()
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 16:27

























        answered Nov 14 '18 at 16:20









        TheIncorrigible1TheIncorrigible1

        9,77431434




        9,77431434
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303222%2fpowershell-script-to-run-exe-file-with-parameters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues

            Fabienne KOHLMANN