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

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 943
    windows 10 professional 64-bit, 22H2
    Thread Starter
       #11

    Try3 said:
    No. The opening command of that batch file calls up the request for admin privilages and that is why you got an admin prompt that you had to accept in order to run the batch file.

    Denis
    Thank you. I seem to need to find a short course on writing batch files. I appreciate your information & patience.
      My Computers


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

    phrab said:
    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
    Good

    phrab said:
    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.
    It would not run as admin unless you had made it run as admin [and had approved it at the admin dialog]
    Be careful what your refer to as running "successfully". If the commands ran "successfully" then you would have finished the job.
    Regsvr32 does need admin privileges - see Regsvr32 - Register a DLL - SS64

    phrab said:
    If I try to choose "Run as administrator", I get a message that the file doesn't have an app associated with it.
    How are you getting a run as admin option? It should not be there for ps1 files. If it is there, because you added this to your context manu manually, then ignore it because it will not do what you think it does.

    phrab said:
    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.
    This test was completely meaningless. It was a waste of your time.

    phrab said:
    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"
    Good.

    phrab said:
    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.
    When you double-click what - the shortcut?
    Did the admin prompt dialog appear?
    Stick an extra line at the end of the ps1 file as a test - just write the word pause so that the window should remain open when the commands finish.
    You shouldn't be getting any errors at this stage because you tested the script in a powershell window earlier on - unless I misunderstood you.

    Denis
      My Computer


  3. Posts : 16,783
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #13

    Batch file, PowerShell guides


    phrab said:
    Thank you. I seem to need to find a short course on writing batch files. I appreciate your information & patience.
    Personally, if I was going to sit down and study something it would be powershell not individual commands in batch files.

    Here's an introduction to batch files - The Command Line in Windows Batch file basics
    And here are some guides by the excellent RobvdWoude
    Batch files - RobvdW
    Batch File Scripting Techniques - RobvdW
    Batch Files Examples - RobvdW
    and some reference guides on individual commands
    <command> /? produces an up-to-date Help file for most commands, this is the only known source for up-to-date, Windows10-specific Help.
    Command-Line Reference - MSDocs {note that the guidance might reflect variants of the command such as older versions or non-WIndows10 versions}
    Windows command index - SS64 {note that the guidance might reflect variants of the command such as older versions or non-WIndows10 versions}
    Commands forum - SS64
    Just to emphasise the point, the Windows 10 netsh command is a significant step forward from its earlier versions but there is no online guide for this version. All online netsh guides are out-of-date or refer to non-Windows10 variants of the command.


    And powershell guides -
    PowerShell Scripting - The Basics - TenForumsTutorials
    PowerShell Scripting - Run a Script from Shortcut - TenForumsTutorials
    PowerShell commands index - SS64
    PowerShell forum - SS64
    PowerShell Tutorials - Windows10Forums [2017] - I am normally wary of this website for reasons that I cannot remember [lots of content copied from this forum possibly?] but their PS tutorial seems OK.
    Mastering PowerShell with Dr. Tobias Weltner [2009] - I've just added this one. Its original website was no longer available so I had to track it down.
    PowerShell Commands - ScriptRunner

    And remember - if it doesn't feel like you're banging your head against the wall, it isn't proper programming.

    All the best,
    Denis
    Last edited by Try3; 10 Jan 2021 at 06:37.
      My Computer


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

    Manipulating variables in batch files


    When you read other people's batch files, you will see variables used in ways that seem peculiar & unintelligible.
    - For Windows commands and batch files, variables are enclosed in % and sometimes %% [and you might sometimes even see %%%]
    - You might see, and this is just for example, Set MyVar=%MyVar:*cd=% This does actually mean something and can be very useful. It is one of the examples explained below.
    - Once you get the hang of manipulating variables in batch files you will be able to write very powerful procedures.
    - Once you get the hang of manipulating variables in batch files you will be able to adapt very easily to manipulating variables in better structured scripting such as powershell.

    This is a guide to manipulating batch files variables that I put together [based on a source whose identity I have rudely mislaid - it might have been RobvdW or an early version of Windows Batch Scripting - Wikibooks]
    - Downloadable version ManipulatingVariables-UsefulExamples.bat.txt
    - The file ManipulatingVariables-UsefulExamples.bat.txt is deliberately laid it out so it can be saved for reading as a text file [.txt] or for running as an innocuous batch file [.bat] - it is just a demonstrator, it displays variables onscreen but does not alter anything else at all. So it is safe to run. Just change its file extension for what you want on any given occasion [txt/bat] - no other changes are necessary.
    - It pauses after every example so you can examine each one in sequence.

    Code:
    REM  This file can be read as a text file by keeping its file extension as .txt or run as a batch file by changing its file extension to .bat {the contents do not need changing}
    REM  The batch file is an innocuous demonstrator - it displays variables onscreen but does not alter anything outside of itself
    REM  I end up referring to this file for almost every batch script I write
     
    
    
    REM  I think my original source for learning to manipulate variables was 'Windows Batch Scripting - Wikibooks' Windows Batch Scripting - Wikibooks, open books for an open world
    REM - and it was that source that inspired me to compile this ready-reference list of examples
     
    
    
    REM The MyVar variable is used as the input in all the examples that follow
    REM and the output variable for each example is NewVar
    REM except for the 'Test for' examples - these just refer to MyVar 
     
    
    
    REM ::::: The variable used as the example :::::
    Set MyVar=abcdefghijklm
    Echo %MyVar%
    REM MyVar echo is abcdefghijklm
    Pause
     
    
    
    REM ::::: Extracting characters :::::
    
    REM Extract the first character
    REM Note - The first character is at location 0 [not location 1]
    Set NewVar=%MyVar:~0,1%
    Echo %NewVar%
    REM NewVar echo is a
    Pause
    
    REM Extract the second character
    Set NewVar=%MyVar:~1,1%
    Echo %NewVar%
    REM NewVar echo is b
    Pause
    
    REM Extract the second and third characters
    Set NewVar=%MyVar:~1,2%
    Echo %NewVar%
    REM NewVar echo is bc
    Pause
    
    REM Extract the last character
    Set NewVar=%MyVar:~-1%
    Echo %NewVar%
    REM NewVar echo is m
    Pause
    
    REM Extract the last two characters
    Set NewVar=%MyVar:~-2%
    Echo %NewVar%
    REM NewVar echo is lm 
    Pause
    
    REM Extract the second though to second-to-last characters
    Set NewVar=%MyVar:~1,-1%
    Echo %NewVar%
    REM NewVar echo is bcdefghijkl
    Pause
    
    REM Extract the second though to third-to-last characters
    Set NewVar=%MyVar:~1,-2%
    Echo %NewVar%
    REM NewVar echo is bcdefghijk
    Pause
     
    
    
    REM ::::: Removing characters :::::
    
    REM Remove the first character
    Set NewVar=%MyVar:~1%
    Echo %NewVar%
    REM NewVar echo is bcdefghijklm
    Pause
    
    REM Remove the first and second characters
    Set NewVar=%MyVar:~2%
    Echo %NewVar%
    REM NewVar echo is cdefghijklm
    Pause
    
    REM Remove the last character
    Set NewVar=%MyVar:~0,-1%
    Echo %NewVar%
    REM NewVar echo is abcdefghijkl
    Pause
    
    REM Remove the last two characters
    Set NewVar=%MyVar:~0,-2%
    Echo %NewVar%
    REM NewVar echo is abcdefghijk
    Pause
    
    REM Remove all occurences of a particular character
    REM For this example, I have chosen to remove c
    Set NewVar=%MyVar:c=%
    Echo %NewVar%
    REM NewVar echo is abdefghijklm
    Pause
    
    REM Remove all occurences of a particular character sequence
    REM For this example, I have chosen to remove bc
    Set NewVar=%MyVar:bc=%
    Echo %NewVar%
    REM NewVar echo is adefghijklm
    Pause
    
    REM Remove all characters up to and including the first occurence of a particular character
    REM This can only be used from the start of a string not in mid position or from the end 
    REM For this example, I have chosen to remove everything up to and including c
    Set NewVar=%MyVar:*c=%
    Echo %NewVar%
    REM NewVar echo is defghijklm
    Pause
    
    REM Remove all characters up to and including the first occurence of a particular character sequence
    REM This can only be used from the start of a string not in mid position or from the end 
    REM For this example, I have chosen to remove everything up to and including cd
    Set NewVar=%MyVar:*cd=%
    Echo %NewVar%
    REM NewVar echo is efghijklm
    Pause
    
    
     
    REM ::::: Testing for particular characters :::::
    REM Note that == means compare text values of variables
    
    REM Test for the presence of a particular character in the string
    REM Note - Only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see Removing characters above]
    REM Notice that this example compares an extract from MyVar with the whole of MyVar
    REM For this example, I have chosen to test for b
    If Not "%MyVar:b=%"=="%MyVar%" Echo %MyVar% contains b
    REM MyVar echo is abcdefghijklm contains b
    REM  What this does is compare an extract of (Myvar with all letter b characters removed) with (the whole of MyVar)  
    REM - if the extract and the whole variable match then there were no letter b characters in the variable in the first place
    REM - if the extract and the whole variable do not match then there was at least one letter b character removed from the variable
    Pause
    
    REM Test for the presence of a particular character sequence in the string
    REM only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see Removing characters above]
    REM The characters are chosen using the Extract character[s] example syntax above
    REM Notice that this example compares an extract from MyVar with the whole of MyVar
    REM For this example, I have chosen to test for bcd
    If Not "%MyVar:bcd=%"=="%MyVar%" Echo %MyVar% contains bcd
    REM MyVar echo is abcdefghijklm contains bcd
    REM  What this does is compare an extract of (Myvar with all letter bcd characters removed) with (the whole of MyVar)  
    REM - if the extract and the whole variable match then there were no letter sequences bcd in the variable in the first place
    REM - if the extract and the whole variable do not match then there was at least one letter sequence bcd removed from the variable
    Pause
    
    REM Test for a particular character sequence at the start or another specific location within the variable
    REM The characters are chosen using the Extract character[s] example syntax above
    REM This particular example tests the two characters at the start of the variable
    REM Notice that this example compares (an extract from MyVar) with (the whole of MyVar)
    REM For this example, I have chosen to test for ab at the start of the variable
    If "%MyVar:~0,2%"=="ab" Echo %MyVar% starts with ab
    REM MyVar echo is abcdefghijklm starts with ab
    Pause
     
    
    
    REM ::::: Replacing characters :::::
    
    
    
    REM Replace all occurences of a particular character - first of two examples of this
    REM For this example, I have chosen to replace c with X
    Set NewVar=%MyVar:c=X% 
    Echo %NewVar%
    REM NewVar echo is abXdefghijklm 
    Pause
    
    REM Replace all occurences of a particular character - second of two examples of this
    REM For this example, I have chosen to replace c with XYZ
    Set NewVar=%MyVar:c=XYZ% 
    Echo %NewVar%
    REM NewVar echo is abXYZdefghijklm 
    Pause
    
    REM Replace all occurences of a particular character sequence - first of two examples of this
    REM For this example, I have chosen to replace bc with XY
    Set NewVar=%MyVar:bc=XY%
    Echo %NewVar%
    REM NewVar echo is aXYdefghijklm
    Pause
    
    REM Replace all occurences of a particular character sequence - second of two examples of this
    REM For this example, I have chosen to replace bc with X
    Set NewVar=%MyVar:bc=X%
    Echo %NewVar%
    REM NewVar echo is aXdefghijklm
    Pause

    Denis
    Last edited by Try3; 03 Apr 2020 at 16:22.
      My Computer


  5. Posts : 943
    windows 10 professional 64-bit, 22H2
    Thread Starter
       #15

    Try3 said:
    Personally, if I was going to sit down and study something it would be powershell not individual commands in batch files.

    Here's an introduction to batch files - The Command Line in Windows Batch file basics
    And here are some guides by the excellent RobvdWoude
    Batch files - RobvdW
    Batch File Scripting Techniques - RobvdW
    Batch Files Examples - RobvdW

    And powershell guides -
    PowerShell Scripting - The Basics - TenForumsTutorials
    PowerShell Scripting - Run a Script from Shortcut - TenForumsTutorials
    PowerShell commands index - SS64
    PowerShell Tutorials - Windows10Forums - I am wary of this website for reasons that I cannot remember [lots of content copied from this forum possibly?] but their PS tutorial seems OK.

    And remember - if it doesn't fell like you're banging your head against the wall, it isn't proper programming.

    All the best,
    Denis
    Thank you so much!! This is terrific.
      My Computers


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

    phrab,

    I have been changing some text within the code of my last post [onscreen & its downloadable version] in an attempt to improve the explanations. I have now stopped fiddling so my last post now contains the final versions.

    It's the first time I have edited that file since 2013 so I suppose it was due.

    Denis
      My Computer


  7. Posts : 16,783
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #17

    I have just added another PS book to the list

    Mastering PowerShell with Dr. Tobias Weltner [2009] - Its original website was no longer available so I had to track it down.

    Denis
      My Computer


  8. Posts : 16,783
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #18

    phrab,

    I have added some guidance about using standard passable variables with batch files at
    my ditty and demo for Standard passable variables [post #47] - TenForums

    Denis
      My Computer


  9. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #19

    Try3 said:
    When you read other people's batch files, you will see variables used in ways that seem peculiar & unintelligible.
    - For Windows commands and batch files, variables are enclosed in % and sometimes %% [and you might sometimes even see %%%]
    - You might see, and this is just for example, Set MyVar=%MyVar:*cd=% This does actually mean something and can be very useful. It is one of the examples explained below.
    - Once you get the hang of manipulating variables in batch files you will be able to write very powerful procedures.
    - Once you get the hang of manipulating variables in batch files you will be able to adapt very easily to manipulating variables in better structured scripting such as powershell.

    This is a guide to manipulating batch files variables that I put together [based on a source whose identity I have rudely mislaid - it might have been RobvdW or an early version of Windows Batch Scripting - Wikibooks]
    - Downloadable version ManipulatingVariables-UsefulExamples.bat.txt
    - The file ManipulatingVariables-UsefulExamples.bat.txt is deliberately laid it out so it can be saved for reading as a text file [.txt] or for running as an innocuous batch file [.bat] - it is just a demonstrator, it displays variables onscreen but does not alter anything else at all. So it is safe to run. Just change its file extension for what you want on any given occasion [txt/bat] - no other changes are necessary.
    - It pauses after every example so you can examine each one in sequence.

    Code:
    REM  This file can be read as a text file by keeping its file extension as .txt or run as a batch file by changing its file extension to .bat {the contents do not need changing}
    REM  The batch file is an innocuous demonstrator - it displays variables onscreen but does not alter anything outside of itself
    REM  I end up referring to this file for almost every batch script I write
     
    
    
    REM  I think my original source for learning to manipulate variables was 'Windows Batch Scripting - Wikibooks' Windows Batch Scripting - Wikibooks, open books for an open world
    REM - and it was that source that inspired me to compile this ready-reference list of examples
     
    
    
    REM The MyVar variable is used as the input in all the examples that follow
    REM and the output variable for each example is NewVar
    REM except for the 'Test for' examples - these just refer to MyVar 
     
    
    
    REM ::::: The variable used as the example :::::
    Set MyVar=abcdefghijklm
    Echo %MyVar%
    REM MyVar echo is abcdefghijklm
    Pause
     
    
    
    REM ::::: Extracting characters :::::
    
    REM Extract the first character
    REM Note - The first character is at location 0 [not location 1]
    Set NewVar=%MyVar:~0,1%
    Echo %NewVar%
    REM NewVar echo is a
    Pause
    
    REM Extract the second character
    Set NewVar=%MyVar:~1,1%
    Echo %NewVar%
    REM NewVar echo is b
    Pause
    
    REM Extract the second and third characters
    Set NewVar=%MyVar:~1,2%
    Echo %NewVar%
    REM NewVar echo is bc
    Pause
    
    REM Extract the last character
    Set NewVar=%MyVar:~-1%
    Echo %NewVar%
    REM NewVar echo is m
    Pause
    
    REM Extract the last two characters
    Set NewVar=%MyVar:~-2%
    Echo %NewVar%
    REM NewVar echo is lm 
    Pause
    
    REM Extract the second though to second-to-last characters
    Set NewVar=%MyVar:~1,-1%
    Echo %NewVar%
    REM NewVar echo is bcdefghijkl
    Pause
    
    REM Extract the second though to third-to-last characters
    Set NewVar=%MyVar:~1,-2%
    Echo %NewVar%
    REM NewVar echo is bcdefghijk
    Pause
     
    
    
    REM ::::: Removing characters :::::
    
    REM Remove the first character
    Set NewVar=%MyVar:~1%
    Echo %NewVar%
    REM NewVar echo is bcdefghijklm
    Pause
    
    REM Remove the first and second characters
    Set NewVar=%MyVar:~2%
    Echo %NewVar%
    REM NewVar echo is cdefghijklm
    Pause
    
    REM Remove the last character
    Set NewVar=%MyVar:~0,-1%
    Echo %NewVar%
    REM NewVar echo is abcdefghijkl
    Pause
    
    REM Remove the last two characters
    Set NewVar=%MyVar:~0,-2%
    Echo %NewVar%
    REM NewVar echo is abcdefghijk
    Pause
    
    REM Remove all occurences of a particular character
    REM For this example, I have chosen to remove c
    Set NewVar=%MyVar:c=%
    Echo %NewVar%
    REM NewVar echo is abdefghijklm
    Pause
    
    REM Remove all occurences of a particular character sequence
    REM For this example, I have chosen to remove bc
    Set NewVar=%MyVar:bc=%
    Echo %NewVar%
    REM NewVar echo is adefghijklm
    Pause
    
    REM Remove all characters up to and including the first occurence of a particular character
    REM This can only be used from the start of a string not in mid position or from the end 
    REM For this example, I have chosen to remove everything up to and including c
    Set NewVar=%MyVar:*c=%
    Echo %NewVar%
    REM NewVar echo is defghijklm
    Pause
    
    REM Remove all characters up to and including the first occurence of a particular character sequence
    REM This can only be used from the start of a string not in mid position or from the end 
    REM For this example, I have chosen to remove everything up to and including cd
    Set NewVar=%MyVar:*cd=%
    Echo %NewVar%
    REM NewVar echo is efghijklm
    Pause
    
    
     
    REM ::::: Testing for particular characters :::::
    REM Note that == means compare text values of variables
    
    REM Test for the presence of a particular character in the string
    REM Note - Only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see Removing characters above]
    REM Notice that this example compares an extract from MyVar with the whole of MyVar
    REM For this example, I have chosen to test for b
    If Not "%MyVar:b=%"=="%MyVar%" Echo %MyVar% contains b
    REM MyVar echo is abcdefghijklm contains b
    REM  What this does is compare an extract of (Myvar with all letter b characters removed) with (the whole of MyVar)  
    REM - if the extract and the whole variable match then there were no letter b characters in the variable in the first place
    REM - if the extract and the whole variable do not match then there was at least one letter b character removed from the variable
    Pause
    
    REM Test for the presence of a particular character sequence in the string
    REM only valid if the variable does not contain quotation marks so any that existed would have to be removed first [see Removing characters above]
    REM The characters are chosen using the Extract character[s] example syntax above
    REM Notice that this example compares an extract from MyVar with the whole of MyVar
    REM For this example, I have chosen to test for bcd
    If Not "%MyVar:bcd=%"=="%MyVar%" Echo %MyVar% contains bcd
    REM MyVar echo is abcdefghijklm contains bcd
    REM  What this does is compare an extract of (Myvar with all letter bcd characters removed) with (the whole of MyVar)  
    REM - if the extract and the whole variable match then there were no letter sequences bcd in the variable in the first place
    REM - if the extract and the whole variable do not match then there was at least one letter sequence bcd removed from the variable
    Pause
    
    REM Test for a particular character sequence at the start or another specific location within the variable
    REM The characters are chosen using the Extract character[s] example syntax above
    REM This particular example tests the two characters at the start of the variable
    REM Notice that this example compares (an extract from MyVar) with (the whole of MyVar)
    REM For this example, I have chosen to test for ab at the start of the variable
    If "%MyVar:~0,2%"=="ab" Echo %MyVar% starts with ab
    REM MyVar echo is abcdefghijklm starts with ab
    Pause
     
    
    
    REM ::::: Replacing characters :::::
    
    
    
    REM Replace all occurences of a particular character - first of two examples of this
    REM For this example, I have chosen to replace c with X
    Set NewVar=%MyVar:c=X% 
    Echo %NewVar%
    REM NewVar echo is abXdefghijklm 
    Pause
    
    REM Replace all occurences of a particular character - second of two examples of this
    REM For this example, I have chosen to replace c with XYZ
    Set NewVar=%MyVar:c=XYZ% 
    Echo %NewVar%
    REM NewVar echo is abXYZdefghijklm 
    Pause
    
    REM Replace all occurences of a particular character sequence - first of two examples of this
    REM For this example, I have chosen to replace bc with XY
    Set NewVar=%MyVar:bc=XY%
    Echo %NewVar%
    REM NewVar echo is aXYdefghijklm
    Pause
    
    REM Replace all occurences of a particular character sequence - second of two examples of this
    REM For this example, I have chosen to replace bc with X
    Set NewVar=%MyVar:bc=X%
    Echo %NewVar%
    REM NewVar echo is aXdefghijklm
    Pause

    Denis
    That really is very nice work indeed Denis.
      My Computer


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

    Paul,

    How very nice of you to say so.
    I assume you found this thread after you looked at Batch file and PowerShell guides [post #16] - TenForums


    All the best,
    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 16:02.
Find Us




Windows 10 Forums