Friday, July 25, 2014

Find the directory path where a running script, PowerShell 3.0 style

Back in the good old days of PowerShell 2.0, we often started our scripts with a line that referred the directory path where the script ran.

This is used e.g. to get location of a WSP package that was to be deployed to the farm. The PowerShell script looked like this.

1 $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
2 $scriptPath + "\mypackage.wsp"
3 #add and deploy package to farm

But in PowerShell 3.0, there is a new build-in variable for this. The “$PSScriptRoot” and now the PowerShell look like this

1 $PSScriptRoot + "\mypackage.wsp"
2 #add and deploy package to farm

This a not a big change to the script, but a line less is a win.

No comments:

Post a Comment