In this batch of FAQs we'll look at a few cool tricks with PowerShell
John Savill's Frequently Asked Questions
October 2, 2017
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 | 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 = @"
You’re PAWSOME!
Keep up the great work!
$($student.FirstName) $($student.LastName) - `$$($student.Paid)
($($student.Grade) - $($student.Teacher))
"@
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
For example I use a counter and insert ever so many items, e.g.
$newpage = @"
"@
$count=0
some kind of loop
{
$count++
if($count -eq 4)
{
$count = 0
$newpage | Out-File $outfile -Append
}
}
About the Author
You May Also Like