John Savill FAQs on IT Pro

In this batch of FAQs we'll look at a few cool tricks with PowerShell

John Savill's Frequently Asked Questions

Three times a week (Monday/Wednesday/Friday), John Savill tackles your most pressing IT questions.

Read through the FAQ archives, or send him your questions via email.

In this batch of FAQs we'll look at a few cool tricks with PowerShell.

Q. How can I check which module a PowerShell cmdlet is in?
Q. I need to create a string in PowerShell that contains double quotes and other characters and I don't want to escape every character.
Q. How can I create a page break in HTML?

Q. How can I check which module a PowerShell cmdlet is in?
Dept - PowerShell

A. Cmdlets are contained within modules. If you wish to check which module contains a particular cmdlet you can look at the details of the cmdlet, e.g.

Get-Command <cmdlet> | fl *
There is an attribute named modulename which shows you the module of the cmdlet.

Q. I need to create a string in PowerShell that contains double quotes and other characters and I don't want to escape every character.
Dept - PowerShell

A. While you can escape characters using the backtick (`) character this could be cumbersome when you have a large amount of them within a large amount of text. Fortunately you can use a verbatim string or as its called in PowerShell a here-string. When using a here-string you can put anything within the string and it will be kept as is including double quotes, new lines, anything.

To use you need to start with @" then a new line and end with "@ on a new line, for example:

$studententry = @"
<h1 class="auto-style1"><span class="auto-style2">You’re PAWSOME!</span><br class="auto-style2" />
<span class="auto-style2">Keep up the great work!</span><br class="auto-style2" />
<span class="auto-style2">$($student.FirstName) $($student.LastName) - `$$($student.Paid)</span><br />
</h1>
<h6 class="auto-style1"><span class="auto-style2">($($student.Grade) - $($student.Teacher))</span><br />
</h6>
<p class="auto-style1"><br />
</p>
"@

In this case all of this would be kept as is BUT the variables I have will still be evaluated which is useful for what I'm trying to do (which is create custom HTML into a page for hundreds of students!

Q. How can I create a page break in HTML?
Dept - PowerShell

A. Often in PowerShell you may output to HTML with the intention to print which means you may need to control when there is a newline which can be tricky with HTML. There are a number of options to force in a new line, the method I prefer is to insert

<p style="page-break-after: always;">&nbsp;</p>

For example I use a counter and insert ever so many items, e.g.

$newpage = @"
<p style="page-break-after: always;">&nbsp;</p>
"@

$count=0

some kind of loop
{

$count++
if($count -eq 4)
{
$count = 0
$newpage | Out-File $outfile -Append
}
}

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish