Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

24 Jan 2011

Get current file in PowerShell

A while ago i wrote a small script to take care of deployment. Configuring the source folders went as following:

param(  
  $BaseDir = (Get-Location).Path,  
  $WebDir = (Resolve-Path "$BaseDir\web"),  
  $DatabaseDir = (Resolve-Path "$BaseDir\database")
)

The problem with this code is that it only works when your current working directory is set to the location of this script. An administrator (or build system) invokes the script as following:

PS C:\Users\Admin>& 'D:\Deployments\20110124\Deploy.ps1';

Because we don’t want to annoy the consumer of our script with the burden of making sure he is in the correct directory we modified our code as following:

param(  
  $BaseDir = (Split-Path $MyInvocation.MyCommand.Definition),  
  $WebDir = (Resolve-Path "$BaseDir\web"),	  
  $DatabaseDir = (Resolve-Path "$BaseDir\database")  
)  

A quick win 🙂