Monday, May 23, 2011

My First PowerShell Script

While reading the Pragmatic Programmer, I felt challenged to learn some sort of scripting language. Here is my first attempt at a PowerShell Script. Granted, it only loops and prints, but it does use some things that could be helpful later (variables, conditions, sleeps, etc.).

$count = 0
while (1 -ne 0)
{
 $count = $count + 1
 echo("Hello World")
 if ($count % 2)
 {
  echo("")
  Start-Sleep -m 1000
 }
 
 if ($count -gt 10)
 {
  break
 }
}

I did learn that running a powershell script file is disabled by default. Typing the command
Get-ExecutionPolicy
will return what the policy is currently set at.
Set-ExecutionPolicy remotesigned
will allow you to run PowerShell script files that you create and will not run other script files, unless the other script has been signed by a trusted publisher. There are other policy levels that this can be set to, but this is all I needed for now.

It will be interesting when I come up with something that I am currently doing that PowerShell can make easier for me.

No comments:

Post a Comment