How can I concatenate a string with a link that has spaces in powershell

Tag: powershell , hyperlink , concatenation , string-concatenation Author: lijingyangxiao Date: 2013-04-03

Here is my code that checks when users passwords will expire and emails them if it will expire in less than 14 days. Since the link in the body of the email has spaces in it, the link does not encompass the whole file name.

#Add the Quest PowerShell snapin
Add-PsSnapIn Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue

#Clear the placeholder log file
Clear-Content O:\logs\network\passwordchangeemails.txt

#Set Email Variables
$today = Get-Date
$logdate = Get-Date -format yyyyMMdd
$emailFrom = "[email protected]"
$body = "Good Morning, `n`n"
$body += "As a courtesy reminder, your Company password will be expiring soon. `n`n"
$body += "If you allow your password to expire, you will be unable to access our network, mail or QAD until you recieve assistance from the Helpdesk. `n`n"
**$body += "To avoid this, please change your network password as soon as possible. (Remote users, please follow the password change procedure for remote users. Click Here -> " + "\\\share\helpdesk\guides\Passwords - Change procedure for remote users.pdf ) `n`n"**
$body += "Feel free to contact the Help Desk by phone at 555-555-5555 or by email at [email protected] `n"
$body += "Thanks!"

#Get Active Directory Information
Get-QADUser -SizeLimit 0 | Select-Object samAccountName,mail,PasswordStatus | 
Where-Object {$_.PasswordStatus -ne "Password never expires" -and $_.PasswordStatus -ne "Expired" -and $_.PasswordStatus -ne "User must change password at next logon." -and $_.mail -ne $null} | 

#For each user, get variables
ForEach-Object {  
  $samaccountname = $_.samAccountName
  $mail = $_.mail 
  $passwordstatus = $_.PasswordStatus
  $passwordexpiry = $passwordstatus.Replace("Expires at: ","")
  $passwordexpirydate = Get-Date $passwordexpiry
  $daystoexpiry = ($passwordexpirydate - $today).Days

      #If days to expire is lessthan 14 days, send email to user
    if ($daystoexpiry -lt 14) {
    $emailTo = "$mail"
    $subject = "Company IT Notification: Your Network password will expire in $daystoexpiry day(s). "    
    Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer
    Write-Host "Email was sent to $mail on $today"
    Add-Content O:\logs\network\passwordchangeemails.txt  "Email was sent to $mail on $today"
$recipients = "[email protected]"
#Copy contents of log file to email and send to IT Department
$content = [IO.File]::ReadAllText("O:\logs\network\passwordchangeemails.txt")
Send-MailMessage -To $recipients -From "[email protected]" -Subject "Password change log for $today" -Body "This is the log from $today, $content" -SmtpServer
Even if the link worked in the first string on that line without concatenation would be fine.

Best Answer

Sorry I can't test this right now but if HTML is OK to use, try wrapping the link as follows...

"<a href=""file:///// - Change procedure for remote users.pdf"">Text you want the users to see goes here</a><br>`n

Then set -BodyAsHtml on Send-Mailmessage:

Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer -BodyAsHtml


That worked perfectly! Thank you!
Good to know, thanks ...& you're welcome, glad to have helped!