You can directly call any PowerShell script from your own code using the following
using Cognifide.PowerShell.Core.Host;
using Sitecore.Data.Items;
namespace MyProject
{
public class TestSPE
{
public void Invoke()
{
using (ScriptSession scriptSession = ScriptSessionManager.NewSession("Default", true))
{
Item speScriptItem = Sitecore.Context.Database.GetItem("/path-or-id/to-spe-item");
string script = speScriptItem["Script"];
if (!string.IsNullOrEmpty(script))
scriptSession.ExecuteScriptPart(script);
}
}
}
}
Be sure to add a reference to Cognifide.PowerShell.dll in your project.
If your script is not stored on a PowerShell Script item in Sitecore then you pass in a string with the SPE commands and script you wish to run.
You can read more in this blog post about Using PowerShell Console for Sitecore in Your Own Code
Update:
To create a "script item", create an item using Template from:
/sitecore/templates/Modules/PowerShell Console/PowerShell Script
This will provide you Script body where you can put your code. You can find examples of the default SPE scripts in the modules Script Library under /sitecore/system/Modules/PowerShell/Script Library.
Update 2:
To get return values from the script, you call to pass false for the stringOutput parameter on the method:
List<object> results;
results = scriptSession.ExecuteScriptPart(script, false);