How to Run a PowerShell Script from Batch File

A friend was lamenting to me recently that there was an aspect of Powershell that he found annoying. He didn’t like the fact that you had to open a PowerShell command line window to run a script. Well guess what, dear friend – you don’t really need to do that!

Purpose:

This blog entry details the quick and easy method for running a PowerShell script from a shortcut on your desktop. To get started let’s have a quick little script on hand to test how this works.

1. Open notepad, copy the below text into it, and save it as PowerShellTest.ps1. Keep track of where you save it.

$name = Read-Host “What is your name?” #Get name from CLI.

$quest = Read-Host “What is your quest?” #Get quest fromCLI

$windowObject = new-object -comobject wscript.shell #Create windows message box object.

$output = $windowObject.popup(“Hello $name.`nYour quest sounds exciting!”,0,”Quest: $quest”,1) #Display the message.

Now, here is how you can open that script in Powershell by just clicking on an icon.

2. Where ever you saved PowerShellTest.ps1, right click and select New Document.

3. Name the file “PowerShellTest.cmd“. The cmd is used to assign a file extension type of command to the text file. This lets it operate as an executable file.

4. You will see the following popup message:

Select “Yes” to confirm.

5. Now right click the PowerShellTest.cmd file that you just made, and select “Edit“. This will open up the CMD file in Notepad, and allow you to finish this process.

6. Inside of the cmd file, type in “Powershell.exe”, a space, and then the name of the PowerShell file you made in step 1.

Notice that I added “.” to the front of the file name. This is required by Powershell to help indicate that this is a file, and not a variable. It is called “Dot Sourcing” in Powershell.

The actual text I used is: Powershell.exe .PowerShellTest.ps1

7. Save and then close the CMD file to continue.

8. Now, to test the final product, double-click on the CMD file that you made in steps 3 and 4. You will get the following result:

You are going to be prompted for your name, and your quest. Just make up something dreadfully clever for each. Once you type the text, press Enter for each line. After your final entry a message box is displayed, indicating the the script has completed.

So, there you go. You were able to run a PowerShell script by simply clicking on a CMD text file. I generally store the CMD file with the PowerShell script file to make them quickly accessible.

Summary:

While this program is simple, it does highlight the method for opening a PowerShell script from Windows Explorer. Incidentally, it shows the method to prompt a script user for input, and also shows how to display a Windows message box output.

As always, let know if you have any questions or comments.

Thank you, Patrick

12 thoughts on “How to Run a PowerShell Script from Batch File

  1. Wow, this is really interesting.You rock!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  2. Finally got to someone who knows what he is talking about. Thanks! your suggested method worked. Appreciate it!

  3. Hey Robi thanks.
    I do want to add one more bit of info. When you run the above method you get a Powershell command window. For this script that was fine. If you were creating a GUI to run in Powershell that would be annoying though.
    The following extra bit of code in the syntax will hide the command window, and result in only the desired GUI window:
    powershell.exe -windowstyle hidden -command .\scriptname.ps1

  4. Hi , very clear and precise … however had to add a backslash as per your pic to make it work and it worked locally ; tried the same from a network location and it failed complaining I think that the script was not found . The script was there in same location! Any idea?

    1. Hey JayOne
      I hope you are having a good day.
      Since I originally posted this I have come across another technique for accomplishing the task of opening Powershell, and running a script without first opening a PS console.
      This technique works across the network as well.

      Make a shortcut to the script you want to run.
      You can invoke the script by calling Powershell in the shortcut.

      Here is an example with a made up server and share name.
      Make the following line the target of the shortcut:
      %windir%\system32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -command \\ServerName\ShareName\DCSCan_GUI.ps1

      The Powershell session is called using: %windir%\system32\WindowsPowerShell\v1.0\powershell.exe

      -windowstyle hidden
      This causes the console not to display in the background.

      -command \\ServerName\ShareName\DCSCan_GUI.ps1
      This is the script that is run in the console.

      This script I called opens a GUI interface, so I don’t really need to see the console. Your script may need direct user interaction, so you wouldn’t have to hide the console.

      I hope this is helpful. Please let me know if you have any questions.

      Thanks
      Patrick

  5. Hi Patrick,

    i am not able to get the GUI Interface, neither the pop-up, after entering the details. any clue why is it happening?

    1. Hello Anu
      Does the GUI run correctly when you try to run it from the PowerShell console?
      If you could send the error message, or a screen shot that would help identify the issue.
      Thank you,
      Patrick

  6. Please revisit this and ensure it is correct. For example, my Powershell does not have a “-windowstyle” option instead I have “-Noninteractive” the example Patrick shows with “-Commad” in it doesn’t include the dot. Also my powershell requires a space after the dot to work, I don’t know if this is different with the various types of a simple typo.

    1. Hello Mark
      What version of PowerShell are you running?
      My current version is version 2. With this version I ran PowerShell /? at the PS prompt. I see both the -Nointeractive and -WindowStyle options available.

      1. Server 2003. Of course the /? doesn’t give a version number for PS so its probably v1. Maybe I need to upgrade it.

Leave a reply to anu Cancel reply