Solved

Help with some formating in a here-string in Powershell

Posted on 2015-01-14
3
90 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 82

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 68

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now