Remove HTML Tags from a PowerShell string or Variable

Scenario:  A variable stores a value that has HTML tags.  You wish to remove all the HTML tags from the variable but keep the content.

Example:   Running the following will store the internal message of Out of Office in the variable $1.

$1 = (Get-mailboxautoreplyconfiguration steve).internalmessage

When displaying $1, it looks like this:

<div class=”BodyFragment”>
<font size=”2″><span style=”font-size:10pt;”><div class=”PlainText”>Steve has left the building. He is never coming back .<br>
</div></span></font>
</div>

 

Scriptlet:

$1 = $1 -replace ‘<[^>]+>’,”

When displaying $1, it will now look like this:

Steve has left the building. He is never coming back.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: