Using a here string in Powershell

Posted on 2012-09-10
Last Modified: 2012-09-11
Greetings Powershell Gurus,

Here is my rookie noob question for the day.  I'm trying to work with a here string.  I want to pull some user information with the get-user commandlet and plug some of that data into a txt file.  Here is what I was thinking, but it isn't working.

$User = get-user | select-object FirstName, LastName, UserPrincipalName

$body = @"
Hello "$user.firstname",

Your username is "$user.firstname"."$user.lastname".
Your email address is: "$UserPrincipalName".


How do I plug in the information I collected with the get-user commandlet?

Am I even close?

Question by:ndalmolin_13
    LVL 11

    Expert Comment

    Do you trying this in domain? Then you must change Get-User to Get-ADUser. The rest looks good.
    LVL 67

    Accepted Solution

    Inside of a here string you need to treat vars and expressions like in normal strings - that is, not enclose them in double quotes, as you already are in "string mode". And because of that, expressions (properties aso.) need to be enclosed in parens:
    $User = get-user | select-object FirstName, LastName, UserPrincipalName
    $body = @"
    Hello $($user.firstname),
    Your username is $($user.firstname).$($user.lastname).
    Your email address is: $($user.UserPrincipalName).

    Open in new window

    LVL 3

    Expert Comment

    Get user as you have it, will pull all your accounts in the domain. You'll need to pull either individual names, or loop through all names in the group.

    If you are looking to output your 'letter' to a txt file, you should be able to write that using something like Out-file. This will create a file for the person you select.

    $User = get-user  "John Doe" 
    $body = @"
    Hello  $($user.firstname),`r`n
    Your username is $($user.samAccountName)`r`n
    Your email address is: $($User.WindowsEmailAddress). `r`n
    $filename = $user.SamAccountName+".txt"
    $Body | Out-file -filepath $filename

    Open in new window

    LVL 1

    Author Closing Comment

    Exactly what I needed to know.  Thanks for the explaination.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
    This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    730 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

    15 Experts available now in Live!

    Get 1:1 Help Now