PowerShell - Multiple get-aduser queries: first result has two leading newlines

CDCR 1 Reputation point
2021-03-20T21:58:43.393+00:00

Found something rather interesting and also annoying if you're trying to have a consistent output.. I don't think there is a solution without M$ changing their source code or me re-writing Get-AdUser as a new function. If you know a work-around, would love to hear about it.

Consider the following:

Function Example1 {
    for ($i=1; $i -le 2; $i++) {
        $TestUsername = "_delete.me"+$i
        Write-Host ("AD User Info: _delete.me"+$i)
        Get-Aduser $TestUsername
    }
}
Function Example2 {
    Write-Host ("AD User Info: _delete.me1")
    Get-Aduser _delete.me1
    Write-Host ("AD User Info: _delete.me2")
    Get-Aduser _delete.me2
}

Example1
Example2

If you call the functions individually you will see two extra newlines on the first return. All subsequent queries have only a single space.

PS C:\Scripts\HDSP> Example1
AD User Info: _delete.me1


DistinguishedName : 

If you call the functions together (select Example1 and Example2) and run selection (F8) the first return has two extra newlines but all subsequent queries have only a single space including the Example2 output.

PS C:\Scripts\HDSP> Example1
Example2
AD User Info: _delete.me1


DistinguishedName : CN=_delete\, me1
Enabled           : True
GivenName         : me1
Name              : _delete, me1
ObjectClass       : user
ObjectGUID        : 4ce4ac23-7f06-486d-bb8e-5601f56267a0
SamAccountName    : _delete.me1
SID               : S-1-5-21-1994435998-1945209534-1039588540-897322
Surname           : _delete
UserPrincipalName : _delete.me1

AD User Info: _delete.me2
DistinguishedName : CN=_delete\, me2@
Enabled           : True
GivenName         : me2
Name              : _delete, me2
ObjectClass       : user
ObjectGUID        : 326daa49-394c-4366-8113-08291c101a3e
SamAccountName    : _delete.me2
SID               : S-1-5-21-1994435998-1945209534-1039588540-897323
Surname           : _delete
UserPrincipalName : _delete.me2

AD User Info: _delete.me1
DistinguishedName : CN=_delete\, me1
Enabled           : True
GivenName         : me1
Name              : _delete, me1
ObjectClass       : user
ObjectGUID        : 4ce4ac23-7f06-486d-bb8e-5601f56267a0
SamAccountName    : _delete.me1
SID               : S-1-5-21-1994435998-1945209534-1039588540-897322
Surname           : _delete
UserPrincipalName : _delete.me1

AD User Info: _delete.me2
DistinguishedName : CN=_delete\, me2
Enabled           : True
GivenName         : me2
Name              : _delete, me2
ObjectClass       : user
ObjectGUID        : 326daa49-394c-4366-8113-08291c101a3e
SamAccountName    : _delete.me2
SID               : S-1-5-21-1994435998-1945209534-1039588540-897323
Surname           : _delete
UserPrincipalName : _delete.me2
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-03-21T02:48:07.49+00:00

    That's the default formatter in PowerShell.

    Usually, PowerShell tries to format an object using Format-Table, but if the output has more than "X" properties the output will be produced by Format-List. IIRC the Format-List and Format-Table) produces one or two empty lines to separate it from other data. Format-Table only does that for the 1st object. Subsequent objects piped to Format-Table (of the same type) won't have a header at all.

    Add to that that you're intermixing output sent directly to the host when you use Write-Host with the output sent to the "Success" stream (what you used to know a STDOUT) and you make things work differently.

    Since the output in you examples are only for display purposes it really matters little. You can format the information any way you like using the "-f" format operator.

    0 comments No comments