I found code to cancel all workflows that are completed using powershell. However I was looking for code (using powershell) that will cancel a workflow (in Progress) on a specific workflow for a specific list when a current item: called "pending" is changed to a different value. What code would work using powershell?
Here is the code I found:
#Your SharePoint Site URL
$web = Get-SPWeb "http://yoursharepointserver.com/yoursubsite";
$web.AllowUnsafeUpdates = $true;
#Your List Name
$list = $web.Lists["YourListName"];
$count = 0
#Loop through all Items in List then loop through all Workflows on each List Items.
foreach ($listItem in $list.Items)
{
foreach ($workflow in $listItem.Workflows)
{
#Disregard Completed Workflows
if(($listItem.Workflows | where {$_.InternalState -ne "Completed"}) -ne $null)
{
#Cancel Workflows
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);
write-output "Workflow cancelled for : " $listItem.Title;
}
}
}
$web.Dispose();