PowerShell changes return object's type PowerShell changes return object's type powershell powershell

PowerShell changes return object's type


What's happening is PowerShell is converting your namespace manager object to a string array.

I think it has to do with PowerShell's nature of "unrolling" collections when sending objects down the pipeline. I think PowerShell will do this for any type implementing IEnumerable (has a GetEnumerator method).

As a work around you can use the comma trick to prevent this behavior and send the object as a whole collection.

function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = ""){    ...    $xmlNsManager.AddNamespace("ns", $NamespaceURI)    return ,$xmlNsManager}


More specifically, what is happening here is that your coding habit of strongly typing $fullyQualifiedModePath is trying to turn the result of the Get (which is a list of objects) into a string.

[string]$foo

will constrain the variable $foo to only be a string, no matter what came back. In this case, your type constraint is what is subtly screwing up the return and making it Object[]

Also, looking at your code, I would personally recommend you use Select-Xml (built into V2 and later), rather than do a lot of hand-coded XML unrolling. You can do namespace queries in Select-Xml with -Namespace @{x="..."}.