Workflow Automation with PowerShell

Workflow upgrades can become a HUGE pain.  Often, the larger the number of running workflows, the larger the pain.  Manual restarts or custom code may be the only solution, both of which are prone to human error and can become expensive. 

From my last post, I prescribed two painkilling design principles that will allow you to automate workflow upgrades:

  • Make your workflows disaster recoverable
  • For every change/upgrade, restart the workflows

Now we can use PowerShell to:

  • Find all workflows running on a given list
  • Stop the existing workflows
  • Start the latest version
  • Report any errored workflows

I’ve included the steps I used to achieve this below for nooobs.  Devs and Infra-dweebs may find PowerShell very refreshing.  It really tends to empower shell-scripting, task automation and development skills you’ve acquired.  For Devs, the hard part will be thinking in terms of AUTOMATING TASKS.  For infra-dweebs, the challenge will be in becoming familiar with the SharePoint Object Model.  Hopefully, the references below will assist both:

1. POWERSHELL INSTALL:  Do nothing yet, just read and install.  Seriously, this is a time saver.

  • Great Getting Started article. 
  • You will need to install Windows Management Framework.  It will require a server reboot.  Don’t forget to set the “Execution Policy”. 
  • PowerShell 2.0 is out at the time of this blogging.  It will still use the 1.0 directory so don’t get confused.  You can check the version here.
  • Other good commands:  Get-Host, Get-Help

2. EDITOR INSTALL:  Use PowerGUI – I couldn’t find anything worthwhile for VS2008 and PowerShell.

  • Install PowerGUI
  • Watch the entire intro video (just kick-arse)
  • If you have time (and patience), watch this video from Mr. Softy.  The speaker is way inefficient but provides a cool introduction using grep and sed.  I’ve listed both examples below incase you skip the 90 MINUTE presentation. 
  • GREP:  cat data.txt | where { $_ -match “Mary” }
  • SED:  cat data.txt | %{$_ -replace “Mary”, “Susan”}

3.  DEVELOPMENT:  Using the tips below, you can literally cut & paste from Visual Studio SharePoint Object Model code. 

  • Conditional logic is a little different but very manageable.  Print out this page and keep it handy as a reference. 
  • Accessing static classs methods/members example: 


  • IMPORTANT!!!!  PowerShell Script Function Calls are like the command line – no parantheses or commas: 

Get-SPItemsInProgress $Site $Web $url $List $Field;

  • IMPORTANT!!!!  PowerShell Class Function Calls are the same as in C# 


  • Disposing of objects still applies in order to prevent COM memory leaks. BE CAREFUL USING COMMAND LINE.
  • Use built-in Try/Catch/Finally – (use $_.Exception.Message; to print exception)
  • Download some SharePoint examples here – although you need to add disposing logic

I create and test most of my PowerShell+SharePoint script in Visual Studio using a C# project.  You just can’t beat the Intellisense and debugging features.  If you’ve gone through the above or already know it all, then take this script for a spin!  As always, let me  know if you have improvements.


~ by spninjablog on January 5, 2010.

2 Responses to “Workflow Automation with PowerShell”

  1. Updated the examples and scripts at
    all SPSite and OpenWeb objects that are called from the CommandLine are Disposed. Thanks for the Link 🙂

    Regards Niklas

  2. I want more, more more….!!

    This is not only the best post on the subject, but its the only one I have found.

    Keep it up!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: