Solved

Help with some formating in a here-string in Powershell

Posted on 2015-01-14
3
93 Views
Last Modified: 2015-01-14
Hello Powershell Experts,

I want to do a proof of concept using Powershell to generate a group membership audit report.  We have a file that has a list of authorized users for the test group.  I then want to compare the list of authorized user to the users actually in the group in Active Directory.  This is easy enough.  I’ve written the following to achieve this goal:

$TargetGroup = "NickTestGroup2"

$AuthorizedGroupMembers = Get-Content -Path C:\PowershellResources\Group2.txt

$GroupMembersInAD = Get-ADGroupMember -Identity $TargetGroup | ForEach-Object {$_.name}

$UnauthorizedGroupMembers = Compare-Object $GroupMembersInAD $AuthorizedGroupMembers -IncludeEqual | Where {$_.sideindicator -eq "<="} | ForEach-Object {$_.Inputobject}

$EmailBody = @"
The following is a list of unauthorized users in the '$TargetGroup':
$UnauthorizedGroupMembers
"@

This almost gives me what I want.  The value of the $EmailBody variable as it is now is:

The following is a list of unauthorized users in the ‘Test Group’:
User1 User2 User3

Ideally, I would like the output to look like:

The following is a list of unauthorized users in the ‘Test Group’:
User1
User2
User3


My question to all of you is:
How can I get my output to is how can I get the content of the $EmailBody variable to list the users on different lines as detailed above?
0
Comment
Question by:ndalmolin_13
3 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40549288
Try this:
$EmailBody = @"
The following is a list of unauthorized users in the '$TargetGroup':
$($UnauthorizedGroupMembers -join "`r`n")
"@

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40549293
You change the default string separator $OFS (which is space by default), or join the strings yourself using a different separator.
$EmailBody = @"
The following is a list of unauthorized users in the '$TargetGroup':
$($UnauthorizedGroupMembers -join "`r`n")
"@ 

Open in new window

0
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 40549902
That worked great.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question