From c39dcfb0f31dbee613d537b5671d48ac2b37ffbd Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 15 Jul 2025 12:12:52 +1000 Subject: [PATCH] Add docs for new .NET MethodInvocation tracing Adds docs around the new .NET MethodInvocation tracing call introduced in PowerShell 7.6. This new tracing method allows you to see what method overload was invoked inside the tracing context. --- .../About/about_Methods.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/reference/7.6/Microsoft.PowerShell.Core/About/about_Methods.md b/reference/7.6/Microsoft.PowerShell.Core/About/about_Methods.md index 0b8b73110a80..53d8a7d8da14 100644 --- a/reference/7.6/Microsoft.PowerShell.Core/About/about_Methods.md +++ b/reference/7.6/Microsoft.PowerShell.Core/About/about_Methods.md @@ -257,6 +257,40 @@ specific overload of the **Bar** method. int: 1 ``` +## Finding what overload was used + +Since PowerShell 7.6, it is now possible to see what method overload was chosen +by PowerShell. This is achieved through the new `MethodInvocation` tracing +command. + +This example shows how to use `Trace-Command` to display the overload chosen +when calling the [String.Split method](/dotnet/api/system.string.split). + +```powershell +Trace-Command -PSHost -Name MethodInvocation -Expression { + "a 1 b 1 c 1 d".Split(1) +} + +Trace-Command -PSHost -Name MethodInvocation -Expression { + "a 1 b 1 c 1 d".Split("1") +} +``` + +```Output +DEBUG: ... MethodInvocation Information: 0 : Invoking method: string[] Split(char separator, System.StringSplitOptions options = System.StringSplitOptions.None) + +a 1 b 1 c 1 d + +DEBUG: ... MethodInvocation Information: 0 : Invoking method: string[] Split(string separator, System.StringSplitOptions options = System.StringSplitOptions.None) +a + b + c + d +``` + +In the first example the `1` was casted as a `[char]` and not a string causing +the split output to split by `[char]1` and not the character `"1"`. + ## Using .NET methods that take filesystem paths PowerShell supports multiple runspaces per process. Each runspace has its own @@ -275,3 +309,4 @@ method. - [about_Member-Access_Enumeration](about_Member-Access_Enumeration.md) - [about_Properties](about_Properties.md) - [Get-Member](xref:Microsoft.PowerShell.Utility.Get-Member) +- [Trace-Command](xref:Microsoft.PowerShell.Utility.Trace-Command) \ No newline at end of file