Just add this before your code in the .ps1 file:
#Load SharePoint PowerShell SnapIn
if ( (Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.Sharepoint.Powershell
}
This will load the required Snapin. Then all the SharePoint commands should work.
If you want to stop a running workflow via Powershell you can try this:
#Site URL
$web = Get-SPWeb "http://urlforsite.com";
$web.AllowUnsafeUpdates = $true;
#List Name
$list = $web.Lists["ListName"];
# Iterate through all Items in List and all Workflows on Items.
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {
#Cancel Workflows
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
}
$web.Dispose();
If you get the error: The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered
, make sure the logged in user who is trying to run SharePoint PowerShell commands is having the right "SharePoint_Shell_Access" to the SharePoint_Configuration database to do so:
- Open SQL Management Studio
- Browse Security folder — Logins , select the user name that is
running power shell , right click properties
- Click on User Mapping , check SharePoint_config , select
SharePoint_Shell_Access
https://stackoverflow.com/questions/7595256/cancel-multiple-sharepoint-workflows-using-powershell