Skip to content

ConvertFrom-String example #5 inaccurately states that types change based on -PropertyNames #741

@joeyaiello

Description

@joeyaiello

Original: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/16796266-convertfrom-string-example-5-is-wrong

Original (incorrect) text :

Example 5: Generate two objects of different object types

PS C:\>"123 456" | ConvertFrom-String -PropertyNames String,Int

This command generates an object with default property names P1 and P2, but property types String and Integer are identified. The results are P1=123 and P2=456. The second property is an integer, not a string.

I've validated that this is true:

In this example, both FirstWord and SecondWord are already byte and int16.

C:\WINDOWS\system32> "123 456" | ConvertFrom-String -PropertyNames FirstWord,SecondWord | Get-Member


   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
FirstWord   NoteProperty byte FirstWord=123
SecondWord  NoteProperty int16 SecondWord=456

In Example 5, the order switches because of alphabetization, and 123 misleadingly looks like a "byte String", but it's actually just a byte called String (just like above):

C:\WINDOWS\system32> "123 456" | ConvertFrom-String -PropertyNames String,Int | gm

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Int         NoteProperty int16 Int=456
String      NoteProperty byte String=123

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions