Help formatting Powershell output

Hello Powershell Experts,
I have an output formatting question.  I have a list of about 40 Active Directory objects that are either users or groups.  I have pieced together some code that will evaluate whether the object is a user or a group.  It will then display which groups the object is part of or display a message that states the object is not part of any groups.  The codes not pretty, but it gets the information I need.  However, it is not formatted the way I would like it.
Here is the code:
$POLObjects= "Poldev"
foreach ($Object in $POLObjects){
    $Data = Get-QADObject $Object | select name, type
    $DataGroupMembership = Get-QADGroup -ContainsMember $Data.name | Select Name
   If ($DataGroupMembership -eq $Null) {$groups = "This user is currently not in any AD groups."}
   Else {$groups = $DataGroupMembership}


Write-Host "The Active Directory object $($Data.name) is a $($data.type)."
Write-Host "$($Data.name) belongs to the following groups:"
Write-host "$($groups.name)"

} 

Open in new window

Here is the output displayed to the screen:
The Active Directory object poldev is a user.
Poldev belongs to the following groups:
Document Owners Document Managers Records Managers Domain Users

Open in new window

Here is what I would like the displayed output to look like:

The Active Directory object poldev is a user.
Poldev belongs to the following groups:
Document Owners
Document Managers
Records Managers
Domain Users

My formatting questions are:
1.       How do I get the groups to be presented in a list?
2.      Can I have the list italicized?
3.      Could I get this sent to a text file as opposed to the screen?

Thanks in advanced for your help.
Nick
LVL 1
ndalmolin_13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
ad 2. No. You can use different colours, though.
ad 3. Yes, but no colour or similar formatting will be retained in a text file. E.g. tabs might not appear the same, etc.
ad 1. You seem to use PowerShell 3 or 4, as your group array is flattened into strings automatically in line 11. With PS 2 you wouldn't see any result.
There are several ways to get the list as list. One is to set $OFS to "`n" - that system var defines which delimiter is inserted between two strings if "serialized" into a single line. With that, and including writing into a file in addition, your code is:
$POLObjects= "Poldev"
foreach ($Object in $POLObjects){
    $Data = Get-QADObject $Object | select name, type
    $DataGroupMembership = Get-QADGroup -ContainsMember $Data.name | Select Name
   If ($DataGroupMembership -eq $Null) {$groups = "This user is currently not in any AD groups."}
   Else {$groups = $DataGroupMembership}

   $OFS = "`n"
   Write-Host "The Active Directory object $($Data.name) is a $($data.type)."
   Write-Host "$($Data.name) belongs to the following groups:"
   Write-host -ForeGround Yellow "$($groups.name)"

   Write-Output "The Active Directory object $($Data.name) is a $($data.type)."
   Write-Output "$($Data.name) belongs to the following groups:"
   Write-Output "$($groups.name)"
} | out-file C:\Temp\Results.txt

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.