Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

Using a here string in Powershell

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".

Regards,
IT
"@

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

Am I even close?

Thanks,
Nick
0
ndalmolin_13
Asked:
ndalmolin_13
1 Solution
 
X_layerCommented:
Do you trying this in domain? Then you must change Get-User to Get-ADUser. The rest looks good.
0
 
QlemoC++ DeveloperCommented:
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).

Regards,
IT
"@

Open in new window

0
 
ericwoodfordCommented:
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
`r`n
Your username is $($user.samAccountName)`r`n
Your email address is: $($User.WindowsEmailAddress). `r`n
`r`n
Regards,`r`n
IT
"@
$filename = $user.SamAccountName+".txt"
$Body | Out-file -filepath $filename

Open in new window

0
 
ndalmolin_13Author Commented:
Exactly what I needed to know.  Thanks for the explaination.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now