I am attempting to get a list of users for the site that are part of a specific domain. The end result will be putting them in a SQL DB, which is fine; I just need to get the array clean.

I am not sure which format I want to use yet.

Get-SPUser -web https://site.domain/site/portal -Limit ALL | Where-Object {$_.UserLogin -like "*DOMAIN\*"} | Format-Table DisplayName, Email -HideTableHeaders

Get-SPUser -web https://site.domain/site/portal -Limit ALL | Where-Object {$_.UserLogin -like "*DOMAIN\*"} | Format-List DisplayName, Email

At this point, the Format-Table and Format-List cmdlets are including extra lines. One quick way to view that it indeed looks at them as items, is to get a count.

For instance, if I set it to a variable to get a count.

$a = Get-SPUser -web https://site.domain/site/portal -Limit ALL | Where-Object {$_.UserLogin -like "*DOMAIN\*"} | Format-Table DisplayName, Email -HideTableHeaders

The result will have an additional 4 lines, so I will need to account for that.

$a.count - 4

There must be a clean way to get these values, as even looping through the array still returns the blank lines.

Foreach ($i in $a){$i}

Any help is appreciated. Thanks.

  • Aren't you getting blank lines because of your first condition Where ? mb you're getting blank displayname/email returned no ? otherwise, you can use a new array whenever $i -ne ''; $newarray += $ifor example.
    – Nico
    Jan 26, 2015 at 14:46
  • @Nico - Even without the Where-Object, if I return everything, the extra line padding is still there. For instance, on a site with only 3 users, the output is like this: BLANK LINE UserLogin : DOMAIN\USER1 UserLogin : DOMAIN\USER2 UserLogin : DOMAIN\USER3 BLANK LINE BLANK LINE
    – raredesign
    Jan 26, 2015 at 15:22
  • What if you use a new array as I wrote ?
    – Nico
    Jan 26, 2015 at 15:31
  • have you tried to run the query without format-table and check the count $a.count?
    – Manu
    Jan 26, 2015 at 15:33

1 Answer 1


This is the expected behavior. You are counting the items in the Format result instead of the actual result.

Format should be used when you want to format the result. In your case the result is

$a = Get-SPUser -web https://site.domain/site/portal -Limit ALL | Where-Object {$_.UserLogin -like "*DOMAIN\*"}

So now if you do $a.count you can see it gives back the correct count.

Instead of piping the result to Format you should pipe it to the next task for updating in the SQL database.

To explain further, if you do below command

    $a = Get-SPUser -web https://site.domain/site/portal -Limit ALL | Where-Object {$_.UserLogin -like "*DOMAIN\*"} | Format-List UserLogin, DisplayName, Email

$a creates a string array and the array contains the output of the Format command.

  • That was a good thought, but does not resolve the issue. The extra lines above and below the output still exist.
    – raredesign
    Jan 26, 2015 at 14:59
  • @raredesign can you try to print the extra information by adding UserLogin also to the Format-List and check what value is there. Jan 26, 2015 at 15:01
  • It's part of the output formatting in both Format-List and Format-Table. I just added UserLogin (| Format-List UserLogin), but the output issue was the same. I don't think it is a matter of any arguments that I output, but is the format itself. Do you have a farm to try this in so you can see what I am talking about? Thanks for the help.
    – raredesign
    Jan 26, 2015 at 15:16
  • @raredesign, now i understood what you are saying. Its the expected behavior. I am updating my answer Jan 26, 2015 at 15:25
  • Thank you! Great explanation. I have accepted your answer. This does leave me in a position where I cannot output the format I would like to include the DisplayName and Email without the UserLogin. The output also now includes default formatting headers, even when I loop through the array. I have tested the output in both console and output text file.
    – raredesign
    Jan 26, 2015 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.