diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 357ce126ab5..bc1f1528900 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4857,10 +4857,22 @@ private static List GetDefaultProviderResults( private static string GetChildNameFromPsObject(dynamic psObject, char separator) { - // The obvious solution would be to use the "PSChildName" property - // but some providers don't have it (like the Variable provider) - // So we use a substring of "PSPath" instead. - string childName = psObject.PSPath ?? string.Empty; + if (((PSObject)psObject).BaseObject is string result) + { + // The "Get-ChildItem" call for this provider returned a string that we assume is the child name. + // This is what the SCCM provider returns. + return result; + } + + string childName = psObject.PSChildName; + if (childName is not null) + { + return childName; + } + + // Some providers (Like the variable provider) don't include a PSChildName property + // so we get the child name from the path instead. + childName = psObject.PSPath ?? string.Empty; int ProviderSeparatorIndex = childName.IndexOf("::", StringComparison.Ordinal); childName = childName.Substring(ProviderSeparatorIndex + 2); int indexOfName = childName.LastIndexOf(separator); @@ -4871,7 +4883,7 @@ private static string GetChildNameFromPsObject(dynamic psObject, char separator) return childName.Substring(indexOfName + 1); } - + /// /// Takes a path and rebuilds it with the specified variable replacements. /// Also escapes special characters as needed.