Fragtzack
Fragtzack

Reputation: 373

How to output list of objects to a single line with separators

New to PowerShell, great experience so far. How can I format the following cmd pipes to put the display to screen as a single line, with elements separated by ; (to easily allow copy/paste into Outlook)

PS C:\Users\amrgro-dennem04> get-adgroupmember  -Identity "gbl-intel-l3-u" -Recursive|get-aduser -properties Mail|select-object Name

Name
 ----
AMRCVW-EDELSI
AMRMOP-REITES
amrmop-kruges

Basically, want the output to look like this:

AMRCVW-EDELSI;AMRMOP-REITES;amrmop-kruges

Upvotes: 3

Views: 12572

Answers (2)

KERR
KERR

Reputation: 1702

Something like this should help, it's a one liner with two commands chained together:

$test = get-adgroupmember  -Identity "gbl-intel-l3-u" -Recursive|get-aduser -properties Mail|select-object Name ; $test -join '; ';

Upvotes: 0

mklement0
mklement0

Reputation: 437953

Try:

(Get-ADGroupMember "gbl-intel-l3-u" -Recursive| Get-ADUser -Properties Mail).Name -join ';'

Given that you don't need the -Properties parameter, because you're not using the .Mail property, you can simplify to:

(Get-ADGroupMember "gbl-intel-l3-u" -Recursive| Get-ADUser).Name -join ';'

Given that you're only accessing the .Name property and not really filtering by member type:

(Get-ADGroupMember "gbl-intel-l3-u" -Recursive).Name -join ';'

If you're using PSv5.1 or higher (on Windows), you can pipe the above commands to Set-Clipboard (... | Set-Clipboard) to copy the resulting text to the clipboard.
In lower PS versions, you can pipe to the external clip.exe utility instead (... | clip), but note that this invariably appends a line break and that you may run into encoding issues.


Note:

  • In PSv3+, you can extract the property values of a collection's elements simply by using .<propertyName> on the collection itself.

    • A simplified example:
      @( [pscustomobject] @{ one = '1a' }, [pscustomobject] @{ one = '1b' } ).one yields array @( '1a', '1b' )
  • -join ';' then joins the array of resulting property values with ; as the separator to form a single output string.

Upvotes: 4

Related Questions