A “Hello World” script is probably a good starting point.
Start PowerShell from Start Menu –> All Programs –> Windows PowerShell 1.0 –> Windows PowerShell.
At the PowerShell command prompt, the command “Help” shows the various commands and aliases that are available in PowerShell.
The command “Write-Host Hello World” will essentially do the same as the old Command Prompt “echo” command – which is great for our purpose here. But that is too easy… we want a script!
In a text editor, create a text file named “test.ps1” and save it to the current path within PowerShell.
Add the following text to the file and save it.
Write-Host Hello World
Back in the PowerShell window, how do we run the script?
Just typing the “test.ps1” produces the following error.
The term ‘test.ps1′ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:8
+ test.ps1 <<<<
Instead, the name of the script needs to be prefixed with a period followed by a forward or backwards slash.
Run the command “./test.ps1”.
Since we haven’t digitally signed the script, another error appears.
File E:\Documents\test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:10
+ ./test.ps1 <<<<
This error occurs because by default the execution policy that affects all users on a machine is set (in the Windows Registry) such that unsigned scripts are disabled.
The command “Get-ExecutionPolicy” shows the current policy about executing scripts. Currently it is set to “Restricted”.
The command “Get-Help Set-ExecutionPolicy” provides general information about the Set-ExecutionPolicy command.
The command “Get-Help Set-ExecutionPolicy -detailed” provides more detailed information about the Set-ExecutionPolicy command.
The command “Get-Help Set-ExecutionPolicy -full” provides the most information about the Set-ExecutionPolicy command.
The command “Set-ExecutionPolicy –executionPolicy RemoteSigned” will set the policy such that only scripts that are downloaded from the internet are required to be signed by a trusted publisher.
However, on Vista the following error occurs because it requires escalated privileges.
Set-ExecutionPolicy : Access to the registry key ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1ShellIds\Microsoft
.PowerShell’ is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<< -executionPolicy RemoteSigned
The easiest way to change the execution policy in Vista is to start PowerShell from the start menu by right-clicking and selecting “Run as Administrator”. From within that PowerShell the Set-ExecutionPolicy command will work. Close that window after executing the command.
Finally we can then successfully execute the script with the command “./test.ps1”, and it actually works!
Posted by Adam Craven 
