Mostly technical stuff with some interesting moments of life

Notes on Windows PowerShell

  • Is bit slow and creepy ;) but better than command line
  • Running as administrator helps you overcome many of the access denied situations
  • PowerShell script files are simple text files with .ps1 extension
    • To run a script file,
      • Open a PowerShell instance (preferably as administrator) 
      • Type the path to .ps1 file and hit enter
    • If you get an error due to execution-policy is restricted, try
      set-executionpolicy remotesigned 
    • The above will ask for your permission. To avoid that and force use
       set-executionpolicy remotesigned -force
    • If you want this to be done across a Windows HPC cluster using clusrun command
      clusrun /nodes:$nodes powershell set-executionpolicy remotesigned -force
  • To start a new process through a script -
    start-process command "options and args"

    • If you want it to be on the same window and wait for it to complete use
      start-process command "options and args" -NoNewWindow –Wait
  • Invoke a command on a remote machine -
  • Invoke-Command -ComputerName $node -ScriptBlock {Start-Service MyService}

    Handling variables

    • Local variables
    • $x=10

    • Command line parameters
    • $param0=$args[0]

    • Environment (User/System) variables
    • $javaBin=$env:JAVA_HOME + "\bin"

      • A better alternative – number 2 indicates system variable. Use number 1 for user variable
      • $javaHome=[Environment]::GetEnvironmentVariable("JAVA_HOME",2)

      • Similarly to set a value – again number 2 is for system level.
      • [Environment]::SetEnvironmentVariable("Path","$tmp",2)

  • String operations (few)

    • Concatenation
    • $name="John"
      $x="hello " + $name + "! How are you ?"

    • Substring
    • $name.Substring(1) // "ohn"
      $name.Substring(1,2) // "oh"

    • StartsWith
    • $name.StartsWith("hello") // true

    • IndexOf
    • $name.IndexOf("ohn") // 1

    • Length
    • $name.Length // 4

    • Join paths
    • $javaHome=$env:JAVA_HOME // e.g. "C:\Program Files\Java\jdk1.7.0_10"
      $javaBinString=join-path -path $javaHome "bin" // e.g. "C:\Program Files\Java\jdk1.7.0_10\bin"

  • Loops – a good tutorial at

    • For loop
    for($i=1;$i -le 10; $i++) {
      • -le is <=
      • -lt is <
      • -gt is >
      • -eq is == in usual programming language notation

    • Foreach loop
    $jars=ls ($env:TWISTER_HOME + "\lib") *.jar
    foreach ($jar in $jars) {
     $jar=$jar.DirectoryName + "\" +  $jar.Name
  • .NET runtime directory
  • $([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
This is pretty much I found useful for my work. Feel free to suggest any.


  1. Say more about PowerShell being "creepy".
    What does that mean?
    How could we make it less creepy?

    Jeffrey Snover[MSFT]
    Distinguished Engineer and Lead Architect for Windows Server

    1. Aha! IIRC "creepy" was since I ran into a seemingly nothing wrong, but didn't quite work situation when handling environment variables.

      $env:PATH would add Powershell's path (C:\Windows\system32\WindowsPowerShell\v1.0\) by itself (I mean it was not set in PATH variable's value when you open Environment Variables GUI from System Properties). I was using this in a setup script that'll take the current PATH and add Java bin directory to it. The problem was that every time someone ran setup it'll grow unnecessarily.

      I could solve this, without doing any text manipulation, by using [Environment]::GetEnvironmentVariable("PATH",2)

      Another minor issue was when using setx to set variables it had a 1024 character limit, which too was solved with [Environment]::SetEnvironmentVariable("Path","$tmp",2) syntax.

      On how to make it less creepy, I'd say perhaps some notes on such automatic behaviors of commands.

      Anyway, in the end the experience was worth it as I could automate the somewhat complicated setup of Map Reduce framework, Twister (, on Windows HPC without having to end up a graphical wizard :)