Tidy up the PowerShell files.

Now fixed with some basic error handling / re-entrant behaviours.