Converting AD object to a string with Powershell

Posted on 2011-04-25
Last Modified: 2012-05-11
I am trying to retrieve a list of operating systems from all the machines in a particular OU and there is a space after each OS name and i wanted to use TrimEnd to remove it. I don't want to use Replace in case OS name contains a space in the middle.

$ou = [ADSI]"LDAP://OU=My OU,OU=My Group,DC=MyDomain,DC=com"
$alComputers=new-object Collections.ArrayList

foreach ($item in $alComputers){
	#Write-Host $
	$os = $item.operatingsystem.TrimEnd(" ")


Open in new window

I keep getting an error in this line:

$os = $item.operatingsystem.TrimEnd(" ")

What am I doing wrong? I assume $item.operatingsystem is not a string but an object, so what can I do to convert it to a string?
Question by:YZlat
    LVL 13

    Expert Comment

    The code is not correct, here is a working one.
    But I do not see any extra space at the end of the name of the operating systems.

    But in case you have, replace line 6 to:
    $ou = [ADSI]"LDAP://OU=My OU,OU=My Group,DC=MyDomain,DC=com"  
    foreach ($item in $alComputers){  
            # Write-Host $  

    Open in new window

    LVL 35

    Author Comment


    worked for me, but also

    $os = $item.operatingsystem[0].trimend()

    What is the difference between  $item.operatingsystem and $item.operatingsystem[0]?

    LVL 13

    Accepted Solution

    $item.operatingsystem is a System.DirectoryServices.PropertyValueCollection type of object. PowerShell and the underlying ADSI com interface assumes a multivalued property for each AD attribute, so it gives this general datatype for AD attributes. That is why trimend method does not exist. First we have to convert this datatype to string.
    Actually both solutions are OK, if it is a real multivalued property then the indexing is the better option, but here, as operatingsystem is a singlevalued property the tostring method is also perfect.
    LVL 35

    Author Closing Comment

    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Why Use PowerShell? Recently, I was asked to come up with a quick way to find out what versions all of our SQL servers were currently running. Well, we have several thousand so taking a manual approach simply wasn't an option. We also didn't have t…
    Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 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

    10 Experts available now in Live!

    Get 1:1 Help Now