How to run Windows PowerShell Scripts? A Quick Guide.

Is it possible?

I have encounter a situation that execute a same java program with different combination of program arguments. Surely, if you are in linux you can use shell script. What about windows? Yes, windows has Windows PowerShell so i have used windows powershell to do the exact same this if i was in a linux computer. This is what is did.

First thing First.

First you need to change some windows PowerShell default permissions to execute your custom powershell scripts. Reason for they are not enabled in default is security reasons. So, what you can do is after executing your scripts just set to the default settings.



First, run powershell as an administrator. You can do this by just right click on powershell icon and select “Run as Administrator”. Then type following command.

Set-ExecutionPolicy RemoteSigned

Type Y to confirm your setting.

Creating the PowerShell script file

Open your favorite text editor and create a file sample_script.ps1. Note that the extension is .ps1 . Now write your commands. For an example in my case to run my java program (MyProgram) with some arguments it is like this.

java MyProgram 1
java MyProgram 2
java MyProgram 3

Run the script

Just open the Windows PowerShell and go to the required directory. Type the script file name and press enter!

sample_script.ps1

Resetting the Permission

To reset the default permission setting. Just open a PowerShell as an administrator once again and type following command.

Set-ExecutionPolicy Restricted

That’s all. Have a nice day!

Leave a Reply

Your email address will not be published. Required fields are marked *