How do I convert a Powershell object to a string?

Posted on 2008-11-09
Last Modified: 2013-11-14
Hi All,
Powershell question...If I return an attribute e.g
[PS] C:\>Get-MailContact testuser1 | Select-Object DistinguishedName
How do I then convert the output...


into simply a string of "cn=testuser1,ou=Contacts,DC=testdom,dc=com" ?

Thanks in advance for any advice!

Question by:davequinlisk
    LVL 6

    Expert Comment

    One of many possibilities would be:
    Get-MailContact testuser1 | Select-Object DistinguishedName | Out-String
    LVL 18

    Accepted Solution

    Easier way
    Get-MailContact testuser1 | %{$_. DistinguishedName }

    Author Comment

    Thanks for the responses so far.  The reason I ask for this is because I want to append the string into an LDAP:// connection string for a vb movehere method.

    I might be missing something but If I run the code mentioned by BSonPosh and throw it into a variable I don't get the correct output but I get  just a blank line..?

    If I use out-string I get the full distingquised name not just the string I need.  I can select the individual string but there is still whitespace surrounding it when I write-host.

    Hope that makes sense and thanks for the help guys!
    $testvar = Get-MailContact testuser1 | %{$_. DistinguishedName }
    Write-Host $testvar

    Open in new window

    LVL 18

    Expert Comment

    Is there a space between the $_. and Distinguishedname? There is NOT suppose to be

    Author Comment

    Thanks BSonPosh.
    I definitly didn't have a space in there but I must of had a typo of some sort.  Re-Typed it in and all is well!

    Many Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now