0

So, I got this script :

    $userName = Read-Host -Prompt 'Input User ID'
echo "[$userName]"

$Properties =
@(
 'DisplayName',
 'SamAccountName',
 'Enabled',
 'Created',
 'EmailAddress',
 'title',
 'manager',
 'AccountLockoutTime',
 'Department',
 'Description',
 'Division',
 'LastLogonDate',
 'LockedOut',
 'PasswordLastSet'
)

Get-ADUser $userName -Properties $Properties | select $Properties
Get-ADPrincipalGroupMembership $userName | select name

When I execute only the last command:

Get-ADPrincipalGroupMembership $userName | select name

The output is :

name          
----          
Domain Users  
blabla
blabla Users
IT.BG         
blabla users 

But when I run the script the output is changed to :

name : Domain Users

name : blabla

name : blabla Users

name : blabla

name : blabla

name : blabla users

Can someone tell me why this is happening and how can I fix it?

Radoslav
  • 11
  • 3
  • Not sure why this was marked as a duplicate. The duplicate is the exact opposite of what the question is about. The answer doesn't even mention `Format-Table`, which is the required cmdlet to answer this question! – Bacon Bits Aug 10 '17 at 12:04
  • @BaconBits I marked it as a duplicate, because the underlying problem is the same (PowerShell default output formatting), and I believe my answer to the other question explains why it's happening and how it can be mitigated. – Ansgar Wiechers Aug 10 '17 at 12:11
  • @AnsgarWiechers Yeah, and I think that makes it a good reference for explaining PowerShell's default output processing, but not a duplicate question. After all, [this question](https://stackoverflow.com/questions/2249619/how-to-format-a-datetime-in-powershell) and [this question](https://stackoverflow.com/questions/9904352/how-to-create-printf-effect-in-powershell) aren't duplciates even though they're both effectively asking, "How do I use the format operator?". It should only be a duplicate if it the answer in the linked question completely answers the question, right? – Bacon Bits Aug 10 '17 at 17:23
  • Doesn't my answer do that? – Ansgar Wiechers Aug 10 '17 at 17:31
  • @AnsgarWiechers No. The answer to this question requires the use of the `Format-Table` cmdlet. If someone didn't know that the `Format-Table` cmdlet existed, reading your answer would *not* resolve their problem, it would only tell them *why* they were having a problem. That's an explanation, not an answer. It'd be like going to the doctor saying, "My arm hurts," and your doctor spent 20 minutes explaining to you how the body experiences pain. – Bacon Bits Aug 11 '17 at 16:09
  • @BaconBits Quoting from my answer: *"You can force subsequent objects or lists of objects to be displayed as separate tables by piping them through the `Format-Table` cmdlet:"* I explicitly added that bit of information yesterday when marking this question as a duplicate, because I had realized that it was missing before. – Ansgar Wiechers Aug 11 '17 at 16:23
  • @AnsgarWiechers Not cool, dude. You added that edit [yesterday](https://stackoverflow.com/revisions/40344479/4). Literally added 10 minutes *after* my answer. – Bacon Bits Aug 11 '17 at 17:39
  • Yes, I edited the answer yesterday, like I already said (and again today, for that matter). Are you saying I should not make my answers more complete when I notice something is missing? – Ansgar Wiechers Aug 11 '17 at 18:40

1 Answers1

1

It looks like the output system is formatting it as a list instead of a table. It's probably because the previous line is formatting output as a list and not a table.

Try forcing it to be a table:

Get-ADPrincipalGroupMembership $userName | Format-Table -Property name -Autosize
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66