locked
Converting an Array to a formatted string value RRS feed

  • Question

  • I know this has to be so simple, but I can't find the way to do it. I have an array of values, and I'm trying to put them into a string as a single value list so I can put it into an Active Directory attirbute that can only have one value. I can get it to work as a single line, but not as a list. Here is what I have:

     

    $GroupName = "AD-GroupName"
    $UserNames = $(get-adgroupmember -identity $GroupName -Recursive)| foreach {$_.SamAccountName}
    $UserNameList = "$UserNames"
    Set-ADGroup -Identity $GroupName -Replace @{info=$UserNameList}
    

     

    This will put all of the usernames on a single line like this:

    User1 User2 User3 User4 User5

    Ideally I want them formatted like this:

     

    User1
    User2
    User3
    User4
    User5

     

    I also tried something like this, but it returns the name of the object (Microsoft.Powershell.Commands.Internal.FormatEntryData) instead of the username.

     

    [String]$UserNameList = $UserNames | Format-List
    

     

    Any suggestions?

     

    Thanks.

     

     

    Friday, March 25, 2011 4:47 PM

Answers

  • 1) "$UserNames" -replace " ","`n"
    2) $ofs="`n";"$UserNames"
    3) $UserNameList = $UserNames | out-string
    And many others.
    • Marked as answer by QuickTek Friday, March 25, 2011 5:24 PM
    Friday, March 25, 2011 5:00 PM

All replies

  • 1) "$UserNames" -replace " ","`n"
    2) $ofs="`n";"$UserNames"
    3) $UserNameList = $UserNames | out-string
    And many others.
    • Marked as answer by QuickTek Friday, March 25, 2011 5:24 PM
    Friday, March 25, 2011 5:00 PM
  • Thanks for the input. I thought it was going to work because the output in the powershell console is formatted properly, but when setting the AD attribute, it stills puts it on a single line. Is there a way to specify a hard carriage return after each line in the string? I know it can be formatted this way because you can do it manually by hand using the AD GUI.
    Friday, April 1, 2011 3:49 PM