Conflicting with Default Formatting??

The other day I was writing a script to get the distribution group memberships of a given user and output the names to the EMS console. If I type these commands into the shell line by line, it works fine:

$UserGroups = (get-qadmemberof -Identity $UserName | where-object {$_.EmailAddresses `
-like “*domain.com*”} | ft Name -HideTableHeaders)
write-host “$UserName is a member of the following Distribution Groups:”
$UserGroups

However, when I ran my script, it generated this error:

The object of type “Microsoft.PowerShell.Commands.Internal.Format.FormatStartData” is not valid or not in the correct sequence. This is likely caused by a user-specified “f
ormat-table” command which is conflicting with the default formatting.
+ CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException
+ FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputComman

Here is the very simple fix:

$UserGroups = (get-qadmemberof -Identity $UserName | where-object {$_.EmailAddresses `
-like “*domain.com*”} | ft Name -HideTableHeaders | out-string)
write-host “$UserName is a member of the following Distribution Groups:”
$UserGroups

Add “| out-string” to the end of the code that retrieves the list of distribution groups. I honestly don’t know why it makes a difference. If you know, feel free to tell the secret in the comments section!

Leave a Reply

Your email address will not be published.