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?

8
  • 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! Commented Aug 10, 2017 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. Commented Aug 10, 2017 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 and this question 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? Commented Aug 10, 2017 at 17:23
  • Doesn't my answer do that? Commented Aug 10, 2017 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. Commented Aug 11, 2017 at 16:09

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

It worked. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.