diff --git a/src/System.Management.Automation/engine/CommandBase.cs b/src/System.Management.Automation/engine/CommandBase.cs
index 4d90a7c249..3708611df0 100644
--- a/src/System.Management.Automation/engine/CommandBase.cs
+++ b/src/System.Management.Automation/engine/CommandBase.cs
@@ -8,8 +8,7 @@ using System.Management.Automation.Internal;
using System.Management.Automation.Internal.Host;
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
-
-using Dbg = System.Management.Automation.Diagnostics;
+using System.Threading;
namespace System.Management.Automation.Internal
{
@@ -132,6 +131,13 @@ namespace System.Management.Automation.Internal
}
}
+ ///
+ /// Gets the CancellationToken that is signaled when the pipeline is stopping.
+ ///
+ internal CancellationToken StopToken => commandRuntime is MshCommandRuntime mcr
+ ? mcr.PipelineProcessor.PipelineStopToken
+ : default;
+
///
/// The information about the command.
///
diff --git a/src/System.Management.Automation/engine/cmdlet.cs b/src/System.Management.Automation/engine/cmdlet.cs
index 742224c21d..ffe13b7304 100644
--- a/src/System.Management.Automation/engine/cmdlet.cs
+++ b/src/System.Management.Automation/engine/cmdlet.cs
@@ -10,7 +10,7 @@ using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Management.Automation.Internal;
-using Dbg = System.Management.Automation.Diagnostics;
+using System.Threading;
namespace System.Management.Automation
{
@@ -100,6 +100,11 @@ namespace System.Management.Automation
}
}
+ ///
+ /// Gets the CancellationToken that is signaled when the pipeline is stopping.
+ ///
+ public CancellationToken PipelineStopToken => StopToken;
+
///
/// The name of the parameter set in effect.
///
diff --git a/src/System.Management.Automation/engine/pipeline.cs b/src/System.Management.Automation/engine/pipeline.cs
index 91bf435918..191f80e1d8 100644
--- a/src/System.Management.Automation/engine/pipeline.cs
+++ b/src/System.Management.Automation/engine/pipeline.cs
@@ -7,6 +7,7 @@ using System.Management.Automation.Runspaces;
using System.Management.Automation.Tracing;
using System.Reflection;
using System.Runtime.ExceptionServices;
+using System.Threading;
using Microsoft.PowerShell.Telemetry;
using Dbg = System.Management.Automation.Diagnostics;
@@ -29,6 +30,7 @@ namespace System.Management.Automation.Internal
{
#region private_members
+ private readonly CancellationTokenSource _pipelineStopTokenSource = new CancellationTokenSource();
private List _commands = new List();
private List _redirectionPipes;
private PipelineReader