Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove white space from PowerShell output

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?

like image 936
cvandal Avatar asked Jan 11 '23 22:01

cvandal


2 Answers

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()
like image 50
mjolinor Avatar answered Jan 20 '23 12:01

mjolinor


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.

like image 44
Keith Hill Avatar answered Jan 20 '23 12:01

Keith Hill