question

CDCR-7404 avatar image
0 Votes"
CDCR-7404 asked RichMatheisen-8856 answered

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.