Converted an object to a string, now how to trim it?

I catch many more properties than these two, but showing only these two for brevity.
The report works. But the ControllerDNSName is not getting truncated. My usage of -Width 8 is being ignored.

$Properties = (Get-BrokerSession -AdminAddress xxxxxxxxx | Select UserFullName, ControllerDNSName| WHERE {$_. UserFullName -eq $User} )

$STR = $Properties.ControllerDNSName

$DCstr = Out-String -InputObject $STR -Width 8 #This is being ignored

New-Object -Typename PSObject -property @{
‘User’ = $Properties.UserFullName
‘DC’ = $DCstr
} | Select-Object ‘User’, ‘DC’
}

$Results | ConvertTo-HTML | Out-file $Report

I looked up WIDTH in this context and read that “The Width parameter applies only to objects that are being formatted.”
Is HTML output not a format? Or am I misinterpreting the word format in this context?

Could you please format your code as code? Thanks.

You might add the results you get at the moment and the results you’d like to get actually. (Or samples illustrating the results)

Hmmm, the width option of out-string seems meaningless…

PS C:\users\me> '123456789' | Out-String -Width 5
123456789

You might need something like this:

‘123456789’.substring(0,5)

or this:

‘123456789’[0…4] -join ‘’

Not familiar with the cmdlet, but based on the documentation, there is a filter for UserDisplayName. Additionally, you can use calculated expressions to do the rest, something like this:

$results = Get-BrokerSession -AdminAddress xxxxxxxxx -UserFullName $user |
           Select @{Name='User';Expression={$_.UserFullName}},
                  @{Name='ControllerDNSName';Expression={$_.ControllerDNSName}},
                  @{Name='DC';Expression={$_.ControllerDNSName.SubString(0,8)}}

I guess “out-string -width” can only be used like this, on an object, not a string:

PS C:\users\me> [pscustomobject]@{name='1234567890'} | Out-String -Width 8

name
----
12345...


PS C:\users\me> '123456789' | Out-String -Width 5
123456789


PS C:\users\me> [pscustomobject]@{name1='1234567890';name2='1234567890';name3='1234567890';name4='1234567890';
  name5='1234567890'} | out-string -Width 17


name1 : 123456789
        0
name2 : 123456789
        0
name3 : 123456789
        0
name4 : 123456789
        0
name5 : 123456789
        0

See if you can get the results as a list using …

$Results | Format-List -Force

… to determine if the built-in formatters will auto-expand all that is there, just as a sanity check.

This is yielding some good early results. Thanks!

Yep. This SubString approach really does the trick. Plus it’s a new way (to me) for how to write SELECT statements. I like this.