6

I'm returning the mail property of a distribution group within Active Directory using the command below in PowerShell.

Get-ADGroup $GroupName -Properties Mail | Select-Object Mail | Format-Wide

The output looks like (asterisks used to represent white space):

*
*
[email protected]
*
*

Is there any way that I can remove the white space added at the beginning and end of the output?

3 Answers 3

11

I think this should work (V2):

(Get-ADGroup $GroupName -Properties Mail | Select-Object Mail | Format-Wide | out-string).split("`n") -match '\S'

Edit: that's way more complicated than it needs to be.

(Get-ADGroup $GroupName -Properties Mail | Select-Object Mail | Format-Wide | Out-String).trim()
Sign up to request clarification or add additional context in comments.

1 Comment

No change in output :-(
1

That is how PowerShell formats output. I have complained on several occasions about the excess blank lines before and after output. If you want to avoid that, then you format the output yourself. You can do that like so:

$res = @(Get-ADGroup $GroupName -Properties Mail | Select-Object Mail)
for ($i = 0; $i -lt $res.Length - 1 + 4; $i += 4) { 
    "{0,-28} {1,-28} {2,-28} {3,-28}" -f $res[$i].Mail,$res[$i+1].Mail,$res[$i+2].Mail,$res[$i+3].Mail
}

This assumes your current console is 120 chars wide. If it is 80, change the -28 above to -18.

BTW the key point here is that PowerShell deals in objects and when it renders those objects to the screen it has a formatting engine that determines things like blank lines before and after the output. If you don't like PowerShell's default formatting, you're free to format objects (displaying whichever properties you want) as you please but it is a bit more work.

All that said, if the command returns only one object, why not just do this:

(Get-ADGroup $GroupName -Properties Mail).Mail

The Select-Object Mail, Format-Wide and Out-String are not necessary. Heck, with PowerShell V3 this will work even if the command returns multiple objects.

5 Comments

I tried your solution and received the following error: Unable to index into an object of type System.Management.Automation.PSObject.
You only get one result back? OK try it again with the updated answer.
That would happen if Get-ADGroup $GroupName -Properties Mail | Select-Object Mail returns nothing. Type in $res[0]. If that outputs nothing then your Get-ADGroup command returned nothing. Do you still have $GroupName defined correctly?
Nevermind, I was using the property Name as I was testing against Get-Process. Changed the property to Mail so it should work now.
That removes the two empty lines from the start, displays the email correctly, but only removes one of the two empty lines from the end.
0

A combination of the examples in the checked answer worked for me in a similar situation:

 ... Format-Table | Out-String).split("`n").trim()

After re-reading the original question it seems I had a different problem to solve. I was looking for a way to trim white space from the end of output lines. The checked answer led me to try the above code which did what I was looking for.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.