Windows PowerShell – Hello World (2)

With Windows PowerShell it is easy to enter a command at the command prompt to return “Hello World!” Part 1 covered how to do that. The code from Part 1 could be saved in a file with a .ps1 extension and it could be executed by calling it from the command prompt. For example, if saved as hello.ps1, the command to call this script would be ./hello.ps1 if you are in the same directory as the file hello.ps1.

If you have installed Windows PowerShell and have not tried running a script then there may be one extra step that you will need to take. It is described in detail at this link at technet.microsoft.com. What this article details I will summarize – the command you will need to enter at the command prompt is:

set-executionpolicy remotesigned

and that will allow you to run your script. Use Notepad or some other simple text editor for actually entering the scripts otherwise you will introduce various control characters that cause PowerShell to digitally puke.

Now for a Hello World script with a little more sophistication. Paste this into a file named ‘hello2.ps1′ and remember to use Notepad.
[reflection.assembly]::loadwithpartialname('system.windows.forms')
[system.Windows.Forms.MessageBox]::show('Hello World!')

My reference for this is http://powerscripting.wordpress.com/2008/03/03/one-liner-pop-up-a-message-box/.

After saving that file you can execute it by entering ./hello2.ps1 at the Windows PowerShell prompt if you are in the same directory as hello2.ps1 – otherwise use the entire path to call hello2.ps1. Your pop-up box will resemble the one below.

Hello World Pop-Up Box


Leave a comment