ToString vs Out-String

Mikhail Firsov 1,881 Reputation points
2021-06-29T10:39:27.51+00:00

Hello,

Would anybody please explain to me why do these two "methods" work differently - I failed to find any information that would clarify this...
110217-00.png

Thank you in advance,
Michael

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-06-29T13:29:27.96+00:00

    Hi,

    Types commonly override the Object.ToString method to return a string that represents the object instance. For your above case, the type of $a1 is DateTime and the method is DateTime.ToSting that converts the value of the current DateTime object to its equivalent string representation. If the type doesn't override the ToSting method, it still calls the default Object.ToString() method. You can get the type using the GetType() method.

    https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tostring

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,426 Reputation points
    2021-06-29T10:47:18.137+00:00

    why do these two "methods" work differently

    Because they are different :-)

    Object.ToString is a .NET method and the objects decides what to return as string representation. If the object has nothing meanfull to return, then it returns it's name.

    Out-String is a PowerShell cmdlet, which collects all string properties of an object and returns it as one accumulated string; see Exmaple 1.

    0 comments No comments

  2. Mikhail Firsov 1,881 Reputation points
    2021-06-29T11:05:05.397+00:00

    "If the object has nothing meanfull to return, then it returns it's name." - the problem is I can't understand/predict when this method has or does not have something "meanfull to return"...

    In other words I don't understand why in my example the "PredictFailure False" can not be cast to string :(

    0 comments No comments

  3. Anonymous
    2021-06-29T12:20:24.563+00:00

    Hi,

    The explanation is given in the above documentation. The default implementation of the ToString method returns the fully qualified name of the type of the Object.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Mikhail Firsov 1,881 Reputation points
    2021-06-29T12:39:00.897+00:00

    "The default implementation of the ToString method returns the fully qualified name of the type of the Object."- that's exactly that l don't understand: I don't think users would like to use ToString just to get the "the fully qualified name of the type of the Object" - conversion to string is expected, not just displaying the name of the object.

    Another example:
    110302-11.png

    If the default implementation of the ToString method returns the fully qualified name of the type of the Object why in this case the real string is returned and not the name of the object as in the first example?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.