jump to navigation

PowerShell script modularization June 10, 2008

Posted by reddogaw in PowerShell.
Tags: , , ,
add a comment

PowerShell has been a sneaky beast whose power is just now starting to creep into my programming psyche. Being the good OO citizen that I am I immediately started wanting to modularize my PowerShell scripts and functions for maximum re-use and easy organisation.

There are a number of ways to modularize your PowerShell including (but probably not limited to!):

Love,
Reddog.

Getting the current script directory in PowerShell June 4, 2008

Posted by reddogaw in PowerShell, Tips.
Tags: , , , ,
1 comment so far

One thing to remember when developing PowerShell scripts is that your current value in $PWD is the process’ current directory not the one where the script was stored.

In my desire to break out a few functions into a common file in order to load them “dot-sourced” (so that the functions become global) I realised I needed help!

In comes a new function from the PowerShell Team Blog. It’s going straight into my PowerShell Profile: Get-ScriptDirectory. It does exactly as the name says, it gets the current directory that script is executed from.

<snip>

  1. function Get-ScriptDirectory
  2. {
  3. $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  4. Split-Path $Invocation.MyCommand.Path
  5. }

</snip>

So that now from my script that requires the Common-Functions.ps1 (located in the same directory) I can call:

  1. # Load up our common functions
  2. $commons = Join-Path (Get-ScriptDirectory) “Common-Functions.ps1″;
  3. . $commons;

Love,
Reddog.

Follow

Get every new post delivered to your Inbox.