Workflow Variables – Sorted!

Blog Post created by graham on Jul 6, 2018

Even a short workflow can generate quite a few variables. When you assign a variable to tasks, the names are presented in alphabetical order. The variables list itself displays the order in which they were created. This makes it difficult to find a particular one to delete for example while refactoring or just coming up for a name for a new variable.

It was such an exercise that prompted me to write the attached script which sorts the variables into order, either by Name or Type and Name.

After exporting the workflow to the filesystem, the script edits the workflow and always writes it to Sorted.NWF – so that the original file is preserved should you need to revert to it.

Trying to use pure xml defeated me, so I used the brute-force method of extracting the WorkflowVariables section and converting it to an xml object, sorting it and pasting it back into the workflow file.


Assume the script is in the same location as your downloaded .NWF file

.\Sort-WflowVariables.ps1 .\MyWorkflow.nwf

Will sort the variables in name order


.\Sort-WflowVariables.ps1 .\MyWorkflow.nwf -ByType

Will sort the variables first into their type and then into order of name

This can then be imported back into the designer where you will see the variables listed in the order requested.

As ever with scripts from the internet, please review its contents to ensure that you are happy to run it in your environment.