We help IT Professionals succeed at work.

Powershell: Select AD info and output to email subject displays @{Name=...

clockwood
clockwood asked
on
I am trying to pull user information from Active Directory and send it in an email.

$Sam = read-host "Enter username:"
$FullDisplayName1 = Get-ADUser $Sam | select-object Name
$MessageSubject = "Information for " + $FullDisplayName1 + " : " + $Sam + ". "

The email sends successfully but the subject looks like:

Information for @{Name=John Smith} : jsmith.

How do I remove the @{Name= from the Name in the subject line
Comment
Watch Question

You have to expand into the values, you've said that $FullDisplayName1 is equal to Get-QADUser $SAM, this means that it's a multi valued property.

If you change your next line to this it will probably work:

$MessageSubject = "Information for $($FullDisplayName1.DisplayName) : $Sam."

Author

Commented:
Chris,

That was exactly it! Thank you so much. I will log that away in my PS knowledge.

Thank you
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
It will be more like
$MessageSubject = "Information for $($FullDisplayName1.Name) : $Sam."
since the hash table in $FullDisplayName1 does consist only of the single attribute Name ...
True, didn't spot he'd done select-object Name after the command, however the same principle applies if that was missed off :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.