Syntax for Batch file &/or powershell script to run multiple commands

Page 1 of 3 123 LastLast

  1. Posts : 946
    windows 10 professional 64-bit, 22H2
       #1

    Syntax for Batch file &/or powershell script to run multiple commands


    I wanted a batch file to run the following commands:
    Start Powershell as administrator
    cd "C:\Program Files\Macrium\Reflect"
    regsvr32 RContextMenu.dll
    regsvr32 RShellEx.dll

    I can get as far as:

    Code:
    echo
    Start "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" cd "C:\Program Files\Macrium\Reflect"
    After that I get error messages.

    I was able to create an Autohotkey script that will do this:

    Code:
    Run, Powershell.exe
    Send cd "C:\Program Files\Macrium\Reflect" {enter}
    Send regsvr32 RContextMenu.dll #p {enter}
    Send regsvr32 RShellEx.dll {enter}
    but I don't know the syntax to create a batch file or powershell script to do this.

    Thank you in advance!
      My Computers


  2. Posts : 16,931
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    If, as seems to be the case, your powershell concern is just the run as admin bit then that is easily arranged.
    - Write the PS script you need and save it as a .ps1 file [this is, I think, what you have wriiten as your AutoHotkey script lines 2-4 without the Send (enter) bits]
    - create a shortcut to the ps1 file in the normal manner
    - set the shortcut to run as admin and add to the start of its target file powershell.exe

    Denis
    Last edited by Try3; 03 Apr 2020 at 06:02.
      My Computer


  3. Posts : 946
    windows 10 professional 64-bit, 22H2
    Thread Starter
       #3

    Try3 said:
    If, as seems to be the case, your powershell concern is just the run as admin bit then that is easily arranged.
    - Write the PS script you need and save it as a .ps1 file [this is, I think, what you have wriiten as your AutoHotkey script lines 2-4 without the (enter) bits]
    - create a shortcut to the ps1 file in the normal manner
    - set the shortcut to run as admin and add to the start of its target file powershell.exe

    Denis
    Thanks for your reply & suggestion. I'm able to get Powershell to run as admin. But I don't know the syntax for the batch file.

    I tried:

    Code:
    echo
    Start "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" cd "C:\Program Files\Macrium\Reflect"
    but that doesn't work.
      My Computers


  4. Posts : 16,931
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    That's not what I said
    Last edited by Try3; 03 Apr 2020 at 06:41.
      My Computer


  5. Posts : 16,931
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #5

    SomeBleedinProcedure.ps1
    Code:
    cd "C:\Program Files\Macrium\Reflect"
    regsvr32 RContextMenu.dll
    regsvr32 RShellEx.dll
    Note that I am just taking your word for it that the regsvr32 commands worked in powershell. They must have done if your autohotkey script worked.
    - I thought the full path might be needed for regsvr32 and possibly the dll [unless the dlls are in the current folder].
    - I would have thought you needed the /s switch to stop regsvr32 generating dialog boxes.


    Shortcut to SomeBleedinProcedure.ps1
    1 Make it normally
    2 Shortcut properties - Alter the target field manually
    Code:
    powershell.exe D:\SomeBleedingFolderPath\SomeBleedinProcedure.ps1
    3 Shortcut properties, Advanced - Set the RunAsadmin checkbox

    Denis
      My Computer


  6. Posts : 946
    windows 10 professional 64-bit, 22H2
    Thread Starter
       #6

    Try3 said:
    SomeBleedinProcedure.ps1
    Code:
    cd "C:\Program Files\Macrium\Reflect"
    regsvr32 RContextMenu.dll
    regsvr32 RShellEx.dll
    Note that I am just taking your word for it that the regsvr32 commands worked in powershell. They must have done if your autohotkey script worked.
    - I thought the full path might be needed for regsvr32 and possibly the dll [unless the dlls are in the current folder].
    - I would have thought you needed the /s switch to stop regsvr32 generating dialog boxes.


    Shortcut to SomeBleedinProcedure.ps1
    1 Make it normally
    2 Shortcut properties - Alter the target field manually
    Code:
    powershell.exe D:\SomeBleedingFolderPath\SomeBleedinProcedure.ps1
    3 Shortcut properties, Advanced - Set the RunAsadmin checkbox

    Denis
    Thank you for this explanation! Yes, I misunderstood you. I'm trying to follow your directions, but I'm still confused &
    must have done something wrong.

    1. I created a .txt file on my desktop that contained the following commands:
    cd "C:\Program Files\Macrium\Reflect"
    regsvr32 RContextMenu.dll
    regsvr32 RShellEx.dll

    2. I named it Register Macrium Columns & changed the extension to .ps1

    Right clicking this file & choosing "run with PowerShell" runs the commands successfully. I can't tell if it's running PowerShell as administrator or not because it flashes too quickly, but the blue background tells me it's PowerShell. If I try to choose "Run as administrator", I get a message that the file doesn't have an app associated with it.

    3. I created a shortcut on my desktop to this file by right clicking & dragging it a short distance & choosing "Create shortcuts here”.

    At this point, if I right click & choose "Run with PowerShell", it runs successfully, but I think it's opening a command
    prompt because the background that flashes is black, rather than blue. If I choose "Run as administrator", I get the
    error message mentioned in 2 above.

    4. I right clicked it & chose Properties

    5. I changed the target from:
    "C:\Users\Phil\Desktop\Register Macrium Columns.ps1"
    to
    powershell.exe "C:\Users\Phil\Desktop\Register Macrium Columns.ps1"

    6. I clicked Advanced & ticked the box that said "Run as administrator".

    The target now says: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "C:\Users\Phil\Desktop
    \Register Macrium Columns.ps1"

    However, when I double click it, the commands don't run & I think that a command prompt is flashing because the background is black.

    I must still be doing something wrong.
    Last edited by phrab; 03 Apr 2020 at 13:50.
      My Computers


  7. Posts : 5,326
    Windows 11 Pro 64-bit
       #7

    Download the attached batch script and double click it to execute the batch script.
    Syntax for Batch file &/or powershell script to run multiple commands Attached Files
      My Computer


  8. Posts : 946
    windows 10 professional 64-bit, 22H2
    Thread Starter
       #8

    FreeBooter said:
    Download the attached batch script and double click it to execute the batch script.
    Thank you for this. I have a question though. This opens a command prompt as administrator. Is that what all batch files open?
    Syntax for Batch file &/or powershell script to run multiple commands-clipboard01.png
      My Computers


  9. Posts : 5,326
    Windows 11 Pro 64-bit
       #9

    This batch script executes commands you have asked for with elevated privileges.
      My Computer


  10. Posts : 16,931
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #10

    phrab said:
    This opens a command prompt as administrator. Is that what all batch files open?
    No. The opening command of that batch file requests admin privileges for a powershell session and that is why you got an admin prompt that you had to accept in order to run the batch file.

    By the way, why did you want to run the regsvr32 commands in a pwershell session anyway? You could have done the whole thing in a batch file without using powershell at all.

    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:11.
Find Us




Windows 10 Forums