• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38264
  • Last Modified:

How do I convert a Powershell object to a string?

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!

  • 2
  • 2
1 Solution
One of many possibilities would be:
Get-MailContact testuser1 | Select-Object DistinguishedName | Out-String
Easier way
Get-MailContact testuser1 | %{$_. DistinguishedName }
davequinliskAuthor Commented:
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

Is there a space between the $_. and Distinguishedname? There is NOT suppose to be
davequinliskAuthor Commented:
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

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now