Archive for December, 2011

Using powershell on Windows

December 6th, 2011

Powershell on windows is the same thing as Bash in linux.

Set the prowershell enviroment

The prowershell script file .ps1 is defaultly forbidden to execute with the ExecutionPolicy “Ristricted”. To see your current ExecutionPolicy, open a powershell windown and type:
Get-ExecutionPolicy

There are totally 4 levels of ExecutionPolicy:

  1. Restricted: no .ps1 execution is allowed
  2. AllSigned: .ps1 files can run, but need a signed digital signature. No matter they are written locally or downloaded form Internet.
  3. RemoteSigned: Local .ps1 files can run, but downloaded scripts need a digital signature.
  4. Unrestricted: All scripts can run without digital signature.
  5. Normally, we need to run local scripts, so we set it to “RemoteSigned”:
    Set-ExecutionPolicy remoteSigned -Scope CurrentUser
    With “-Scope CurrentUser”, the ExecutionPolicy will only be changed to the logged user.

    Basics

    Loops

    There are many ways to loop in powershell: do…while…, while(){}, for(){}, …
    Here is an example of while{} loop

    $i = 1
    while ($i -le 5) {
    	Write-Host $i; # write on console
    	mkdirdir x$i # create a folder
    	
    	$i++
    }