none
Run .ps1 scripts with .bat files

    Question

  • I am new to scripting and do not have very good knowledge in scripting.

    I have created a “launch.bat” file to open PS in my script directory and it works perfectly fine.

    Start powershell  -noexit -command cd  D:\Scripts\Myscripts

    I also have some .ps1 scripts in my script folder

    .  . load.ps1

    . Format.ps1

    . install.ps1

     I would like to know if I can run the above scripts one after the other with one single “launch.bat “ file.


    Thanks in advance.

    Saturday, July 09, 2011 1:20 PM

Answers

  • Sure.

    I created 4 PowerShell scripts. Script1.ps1, script2,ps1, script3.ps1 and script4.ps1. script1.ps1 is the main script and it will call script2.ps1, script3.ps1 and script4.ps1

    Here is the content of script1.ps1

    .\script2.ps1
    .\script3.ps1
    .\script4.ps1

    content of script2.ps1

    cls

    content of script3.ps1


    dir \

    content of script4.ps1

    $i = 0
    while ($i -ne 11)
    {
            write-host $i
            $i++
    }

     

     


    Thanks, Wilson Souza - MSFT This posting is provided "AS IS" with no warranties, and confers no rights
    Thursday, July 14, 2011 5:31 AM
    Moderator

All replies

  • Is there a reason you want to start with a .bat script instead of powershell?

    You could do it all within powershell...

     


    Thanks, Wilson Souza - MSFT This posting is provided "AS IS" with no warranties, and confers no rights
    Monday, July 11, 2011 11:40 PM
    Moderator
  • I run these scripts at least five times a day on different machines, and follow the same sequence daily.

    A)    Browse to my script folder "D:\Scripts\Myscripts 

    B)    run   "..load ps1"

    C)    .format.ps1

    D)    .install.ps1

    The second and the third scripts take around 15 MIN to complete.

    I think it is easy to run these scripts with one .bat file at once, rather than running them from PS manually one by one.

    If there is any easy way with PS, i would definitely like it try it.

    Tuesday, July 12, 2011 6:03 AM
  • You can use a PS1 script to call the next or create a 'master'powershell script to call load.ps1, format.sp1 and install.ps1 the same way you would do with the .bat file.
    Thanks, Wilson Souza - MSFT This posting is provided "AS IS" with no warranties, and confers no rights
    Wednesday, July 13, 2011 6:32 AM
    Moderator
  • Hi Wilson,

    Thanks for the reply.

    Can you please give me an example, so that i may be able to create a script myself by referring the example ?

    Thanks in advance.

     

    Thursday, July 14, 2011 4:59 AM
  • Sure.

    I created 4 PowerShell scripts. Script1.ps1, script2,ps1, script3.ps1 and script4.ps1. script1.ps1 is the main script and it will call script2.ps1, script3.ps1 and script4.ps1

    Here is the content of script1.ps1

    .\script2.ps1
    .\script3.ps1
    .\script4.ps1

    content of script2.ps1

    cls

    content of script3.ps1


    dir \

    content of script4.ps1

    $i = 0
    while ($i -ne 11)
    {
            write-host $i
            $i++
    }

     

     


    Thanks, Wilson Souza - MSFT This posting is provided "AS IS" with no warranties, and confers no rights
    Thursday, July 14, 2011 5:31 AM
    Moderator