Format-table makes colums much wider than the content

Hello guys! I’m running the following command:

Get-MpPreference | Select-Object -Property @{N='Station';e={hostname}},
                                           @{n='ExcludedProcess' ; e= {$excl = $_ | Select-Object -expandProperty ExclusionProcess; foreach($line in $excl){$str = $str + $line + "`n"} ; $str }},
                                           @{n='ExcludedPath' ; e= {$excl = $_ | Select-Object -ExpandProperty ExclusionPath; foreach($line in $excl){$str2 = $str2 + $line + "`n"} ; $str2 }} | ft station,ExcludedProcess,ExcludedPath -wrap -autosize

and the displayed columns for ExcludedProcess and ExcludedPath are much more wider than the longest Path or FilePath that they contain, because of that, sometimes the ExcludedPath does not fit and it is completely excluded from the output. I’m just trying to display each path on a different line but within the same text block (one record/object)

Do you have any idea what is the reason for that?

I cannot test on my machine because I don’t have any exclusions but I think what you want will not work. You could try to make it a little easier on the eyes …

Get-MpPreference | 
    Select-Object -Property @{Name = 'Station'; Expression = {$ENV:ComputerName}},
                            @{Name = 'ExcludedProcess'; Expression = {$_.ExclusionProcess -join ', ' }},
                            @{Name = 'ExcludedPath'; Expression = {$_.ExclusionPath -join ', '}} | 
                                Format-Table -Wrap -AutoSize

Thank you Olaf! The problem is that I’m not getting one path per row (which will look much nicer), the paths are listed one after another separated by commas.

That’s right. If you want to change that you would need to restructure the data you get in return what actually gives them another meaning.

What’s the actual purpose of that requirement? Why does it have to look exactly this way? In my experiences most of the time we do not query data to be displayed on the console. Either you need the data for further processing steps in PowerShell - in this case the format does not matter at all - or you need to show the data to some humans - then you may output it as simple lists or format it in Excel to look nice.

1 Like

The purpose is to build custom commands and add them to a module that can be accessed by my colleagues which are not that familiar with PowerShell. the command will display the result on the console as well as save it to a txt and a csv list . The main interest is on the console, so that is why I want the paths on different rows.

I’d consider this as two completely different purposses. You should consider two separate ways of preparing the needed data for the different purposses.

1 Like