locked
Running commands from a PowerShell script RRS feed

  • Question

  • I need to launch a small number of (DOS-like) commands from my .ps1 script

    The commands look like:

    change user /install

    c:\folder1\setup.exe

    change user /execute

    I created a simple .cmd file but i am unable to understand the right syntax to invoke it from my .ps1 file.

    Can anybody please help?

    Can I invoke instead each single command from my .ps1 script?

    Regards

    Mario

    Monday, June 12, 2017 4:08 PM

Answers

  • To invoke commands just put a ".\" before the file name 

    .\filename.BAT


    Monday, June 12, 2017 4:20 PM
  • Hi Mario,

    did you mean this?

    Invoke-Command -ComputerName (Get-Content Servers.txt) -FilePath C:\Scripts\Sample.ps1 -ArgumentList change user /execute, change user /install

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, June 13, 2017 6:31 AM
  • change user /install

    c:\folder1\setup.exe

    change user /execute

    Can I invoke instead each single command from my .ps1 script?

    The commands are part of PowerSHell and Windows: just type them at a prompt or add them to a ps1 file.

    You can type this for any command to see if it is available in PowerShell

    Get-Command change

    All EXE files are available to PowerShell or in a PowerShell script.


    \_(ツ)_/

    • Proposed as answer by jrv Tuesday, June 13, 2017 9:24 AM
    • Marked as answer by Mario - Roma - Italy Friday, June 16, 2017 2:50 PM
    Tuesday, June 13, 2017 9:24 AM

All replies

  • In PowerShell just type

    c:\path\file.cmd

    Have you even tried anything?


    \_(ツ)_/

    Monday, June 12, 2017 4:19 PM
  • To invoke commands just put a ".\" before the file name 

    .\filename.BAT


    Monday, June 12, 2017 4:20 PM
  • Hi Mario,

    did you mean this?

    Invoke-Command -ComputerName (Get-Content Servers.txt) -FilePath C:\Scripts\Sample.ps1 -ArgumentList change user /execute, change user /install

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, June 13, 2017 6:31 AM
  • change user /install

    c:\folder1\setup.exe

    change user /execute

    Can I invoke instead each single command from my .ps1 script?

    The commands are part of PowerSHell and Windows: just type them at a prompt or add them to a ps1 file.

    You can type this for any command to see if it is available in PowerShell

    Get-Command change

    All EXE files are available to PowerShell or in a PowerShell script.


    \_(ツ)_/

    • Proposed as answer by jrv Tuesday, June 13, 2017 9:24 AM
    • Marked as answer by Mario - Roma - Italy Friday, June 16, 2017 2:50 PM
    Tuesday, June 13, 2017 9:24 AM
  • Hi Mario

    did you have any updates for this issue?

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, June 15, 2017 2:16 AM
  • First of all, many thanks tio everybody.

    I think I solved the problem by using a syntax like:

    cmd /c 'change user /install'

    cmd /c 'c:\folder1\setup.exe'

    cmd /c 'change user /execute'

    The only problem I see, at the moment, is that the result of the commands look to dirthy the Return code of the script block containing the code.

    But it's a problem I need to investigate.

    Many thanks, by now

    marius

    Friday, June 16, 2017 2:49 PM