Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: PowerShell Thriller Revisited

Posted on November 16, 2018

thriller-coverA number of years ago I shared a fun PowerShell script that generated a description of a new thriller you might find in the action thriller section of your local book store. I modeled it after the works of authors like Vince Flynn, Ben Coes and James Rollins. I'm a big fan and of these types of books and certainly a guilty pleasure. Because they are genre books there is a certain formula and my script generated a description of a "new" book using data randomly drawn for a collection of different items such as the hero's name, his former profession (in these books the hero is almost always male), the female interest and the villainous organization.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The first iteration was a bit simplistic. Even though I had better things to do, I spent some time updating and expanding the script. As with all my Friday Fun posts, the script is intended as a learning tool. Unless you are a budding thriller author, you really don't have any need for the script itself, other than an amusing diversion. But, if you need to see an example of how arrays are used, or here strings or even how to structure a simple script, my project will help you out.

The current code is written as a script which means you need to specify the full path to the script and of course you need an execution policy that will run the script. Here's an example of what you can expect. The script writes a string object to the pipeline.

image

Because markdown is seemingly everywhere now, this version includes a parameter to convert some of the text into markdown.

image

One of the reasons is because PowerShell Core has a few markdown cmdlets including Show-Markdown which will render a markdown document in the console.

image

It also works in PowerShell Core on Linux.

image

As you can read, I'm having a bit of fun with this. The script is posted as a gist on my Github repo.

In the script I define a number of arrays that contains the source items for the different elements in the book description. Originally I was piping each array to Get-Random to select an element. But I wanted a bit more randomization and also some flexibility. So I wrote a private function that is embedded in the script. This function is never meant to be run directly by a user which is why it does not have a proper name.  Each array is processed by the function to first randomize the entire collection and then select a random number of elements. In some instances I want more than one item.

The main element of the script is a here string, defined as $blurb. A here string is a great way of creating a multi-line string. Just remember that the closing line needs to be left justified. Because this is a string, I can use variable replacement throughout. Although I am also demonstrating how to use the -f operator.  In the here string you'll see placeholders {0} and {1}. The -f operator takes a comma separated list of values and plugs each one into the corresponding placeholder.

$($blurb -f "'$($title[1])'","'$($title[2])'")

The entire expression is defined as a sub-expression because it is being replaced in yet another here string, $out, which will be the script's output. I define a different version of $out depending on whether I need to generate markdown or not.

if ($AsMarkdown) {
    
    $out = @"

# $($Title[0])

## by $author

$($blurb -f "[$($title[1])]()","[$($title[2])]()")
### Published $pubDate 
"@
}
else {
    $out = @"

$($Title[0])
$("-" * ($title[0].length))
by $author

$($blurb -f "'$($title[1])'","'$($title[2])'")
Published $pubDate
"@
}

I've tried to annotate the code to help explain what I am doing and why. If you are learning PowerShell, I hope you'll take some time to analyze how the script works. After you've had some fun generating a few new books!

As always, I love to hear what you think. Enjoy and check the corners.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Friday Fun: PowerShell Thriller Revisited”

  1. Pingback: More Fun with PowerShell Thrillers | The Lonely Administrator

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d