Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:32:57+00:00 2026-06-05T20:32:57+00:00

I have a powershell script that will generate an email for users whose password

  • 0

I have a powershell script that will generate an email for users whose password will expire in <=10 days. The email is formatted into HTML, but I want to change the font color of one sentence to red to call attention to it. However, I’m having a problem with the quotation marks around the html code. If I use double quotes powershell outputs the literal message and gives an error message with single quotes. Is there a way to use multiple font colors in a powershell email?

Here is the code I’m using currently. I’ll also add this is my first attempt at a script in powershell so if I’m doing things the long way I’m open to some input.

    # Import ActiveDirectory module for Powershell V2 AD cmdlets
    import-module activedirectory
    # Uncomment the following line to include optional cmdlets included with Exchange  2010 schema changes. No such cmdlets are included in this script
    # add-pssnapin microsoft.exchange.management.powershell.e2010 

    #Import the maximum password age from Active Directory GPO policy from domain
    $maxdays=(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.TotalDays
    $date = date

    # Simple HTML Message to format body of email. Body is broken up into four parts for appearance and for easy function insertion into message.
    $body1 +=   "<html><body><br> Your network password will expire in "
    $body2 +=   " day(s).</body><html>"
    $body3 +=   "<html><body><br>Employees of Organization, when you receive this email please visit https://scriptlogic/iisadmpwd/aexp2b.asp to reset your network password."
    $body3 +=   "<br>If you are <font color =""#99000"">not employed by Organization</font>, please visit https://gateway.organization.org to reset your network password using our Citrix website."
    $body3 +=   "<br>If you need assistance resetting your password, please contact the Ibformation Service Department at 867-5309"
    $body3 +=   "<br>If you have a portable device, smart phone, etc. that you use to access the Network the new password will need to be updated on these devices also."
    $body3 +=   "<br><br>Thank you,"
    $body3 +=   "<br> IS Department"
    $body3 +=   "<br><img src='P:\Documents\PowerShell\Scripts\password\logo.jpg' alt='logo'/>"
    $body3 +=   "<br><br><hr>"
    $body3 +=   "From <b> IS Department</b>"
    $body3 +=   "<br>The information contained in this e-mail and any accompanying documents is confidential, may be privileged, and is intended solely for the person and/or entity to whom it is"
    $body3 +=   "<br>addressed (i.e. those identified in the <b> To: </b> and <b> cc:</b> box). They are the property of this organization. Unauthorized review, use, disclosure, or copying of this"
    $body3 +=   "<br>communication, or any part thereof, is strictly prohibited and may be unlawful.  The IT Department thanks you for your cooperation.<br>"
    $body4 +=   "<br><hr><br></body></html>"

    # Combine body segments into string for display
    $bod1y=$body1 | out-string 
    $body2=$body2 | out-string 
    $body3=$body3 | out-string 
    $body4=$body4 | out-string 

    #Gather ADusers which are enabled, password is set not set to never expire and all properties of user object. *Note Extension Attributes will not show up unless they are populated.
    (Get-ADUser -filter {(Enabled -eq "True") -and (PasswordNeverExpires -eq "False")} -properties *) | Sort-Object pwdLastSet |

    #Loop to validate password age of each account and generate email. Emails to non-domain addresses are generated based on extensionattribute1 and extensionattribute2. 
    #Active Directory is pre-populated with the user address as extensionattribute1 and domain information in extensionattribute2. For example, johndoe = extensionattribute1
    # gmail.com = extensionattribute2.
    foreach-object {
    $lastset=Get-Date([System.DateTime]::FromFileTimeUtc($_.pwdLastSet))
    $expires=$lastset.AddDays($maxdays).ToShortDateString()
    $daystoexpire=[math]::round((New-TimeSpan -Start $(Get-Date) -End $expires).TotalDays)
    $samname=$_.samaccountname
    $firstname=$_.GivenName
    $lastname=$_.SN
    $extensionattribute1=$_.extensionattribute1
    $extensionattribute2=$_.extensionattribute2
    $recipient="$extensionattribute1@$extensionattribute2"
                if (($daystoexpire -ge 1) -and ($daystoexpire -le 10)) {
                $ThereAreExpiring=$true

                $email = @{
                to = "$recipient" 
                from = 'IS_Notifications@org.org'
                subject = "$firstname $lastname your network password will expire in $daystoexpire day(s)"
                body = "$firstname $lastname" +  " $body1" + "$daystoexpire" + "$body2" + "$body3" + "$date" + "$body4"
                smtpserver = 'smtp.server.org'
                # attachments = "p:\documents\citrix\citrix_password_reset.doc"
            }
        Send-MailMessage @email  -BodyAsHTML
        }

}`

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:32:58+00:00Added an answer on June 5, 2026 at 8:32 pm

    Have you looked at the “here string” feature of Powershell? There is a technet article that discusses the feature. I use them all of the time for strings that are templates for stuff.

    I like to use c#-style placeholders like {0} in such templates. This allows for fancy formatting of dates and currency. (I am using a ‘fancy’ date format in my example.)

    Using a template with placeholders also means I don’t have to remember to concatenate strings together in a particular order or keep in mind where stuff like $firstname has to go within those concatenizations. It’s also supposed to be more easily internationalized, but I’ve never done that.

    Here is a quickie example, you would need to integrate it into your looping logic.

    # first, stick the template into a variable for later use. Use a "here string" for ease of formatting and editting.
    $bodyTemplate = @"
    {0} {1}
    <html><body><br> Your network password will expire in {2} day(s).</body><html>
    <html><body><br>Employees of Organization, when you receive this email please visit https://scriptlogic/iisadmpwd/aexp2b.asp to reset your network password.
    <br>If you are <font color =""#99000"">not employed by Organization</font>, please visit https://gateway.organization.org to reset your network password using our Citrix website.
    <br>If you need assistance resetting your password, please contact the Ibformation Service Department at 867-5309
    <br>If you have a portable device, smart phone, etc. that you use to access the Network the new password will need to be updated on these devices also.
    <br><br>Thank you,
    <br> IS Department
    <br><img src='P:\Documents\PowerShell\Scripts\password\logo.jpg' alt='logo'/>
    <br><br><hr>
    From <b> IS Department</b>
    <br>The information contained in this e-mail and any accompanying documents is confidential, may be privileged, and is intended solely for the person and/or entity to whom it is
    <br>addressed (i.e. those identified in the <b> To: </b> and <b> cc:</b> box). They are the property of this organization. Unauthorized review, use, disclosure, or copying of this
    communication, or any part thereof, is strictly prohibited and may be unlawful.  The IT Department thanks you for your cooperation.<br>
    {3:D}
    <br><hr><br></body></html>
    "@
    
    # Now, loop through your users, calculate $DaysUntilExpiry, test the value and build the email to be sent
    # I'm just making up some dumb values here
    $daystoexpire = 42 # or whatever
    $firstname =  "George"
    $lastname = "Washington"
    $date = date
    
    # using the template, stick the appropriate values into place and store that in a variable for convenience
    $body = $bodyTemplate -f $firstname, $lastname, $daystoexpire, $date
    
    # do whatever we want with $body
    write-host $body
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a PowerShell script that will create an email, however I can't
I have the following PowerShell script that will parse some very large file for
We have a powershell script that creates a local html file and the owner
Does anyone have a PowerShell script that will rename a local user account (Guest
I have a complex Powershell script that gets run as part of a SQL
I have developed a Powershell script that needs to be run automatically daily. This
I have a MSBuild script that starts PowerShell to perform some work on remote
I have a Batch file which will call a Powershell Script : BATCH FILE
I am have a powershell script that does a few things that all need
I have a Powershell script that reads values off of the pipeline: PARAM (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.