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 Valentines Day

Posted on February 10, 2012February 14, 2014

With Valentine's Day just around the corner, I thought I should help you out with your gift giving. Remember those bags of candy with the cute sayings like "Be Mine"? Here's how you can create a "bag" of them using Windows PowerShell; perfect for that extra geeky significant other. Or maybe you'll just have fun playing with this.

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!
#change the window title
$host.ui.RawUI.WindowTitle="Happy Valentine's Day"

#define an array of sentiments
$print=@("Be mine","Hey cutie","Be my valentine","I luv u","I$([char]3)U","Love",
"Romance","Kiss me","Hug me","My Girl","UR the 1","Love me tender","Luv2Luv",
"Hold Me","Sweetie","Honey","HunnyBunny","$([char]3)2$([char]3)","4Ever","XXOOXX")

#define the colors to use for the candy
$color="Cyan","Magenta","Yellow","White","Red","DarkRed","Green"

#enter a number greater than 100 for maximum impact
$count=Read-Host "How many sweethearts do you want?"

#create the candy elements
1..$count | foreach -begin {cls} -process {
 Write-Host "$(get-random $print) " -foreground (get-random $color) -nonewline 
 } -end {write-host "`n"}

The first part of the script changes the console title. Nothing extra special there, in terms of PowerShell. Next, I'm defining an array of strings that will be "printed" on the "candy". I'm also defining an array of colors that you can use with Write-Host. Some colors like Gray hardly seem romantic so I'm limiting my array to the "pretty" colors. Finally, the script prompts you for the number of candies to create.

The main part of the script is a Foreach-Object expression. I'm using the range operator (..) as a counter. Each number is piped to ForEach, but I'm not doing anything with it. What I am doing though is using other cmdlet parameters that you may not be used to seeing. Most of the time when we use ForEach-Object, the scriptblock is the -Process parameter value, which runs once for every object piped in. But in my script I'm also using the -Begin parameter. This scriptblockk executes once before any pipelined objects are processed. In my script I'm simply clearing the screen. In the process script block I'm using Write-Host to write a random string from the $Print array using a random foreground color from the $color array. I'm also using -NoNewLine because I want to fill the screen, (or bag in keeping with my analogy). If I didn't, I'd get a list. But because I'm not writing a new line, when I've reached the maximum number of candies, the -End scriptblock runs which simply adds the necessary return.

Download Valentines.ps1

Happy Valentine's Day.


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

2 thoughts on “Friday Fun PowerShell Valentines Day”

  1. aman dhally says:
    February 10, 2012 at 10:51 am

    Hi “JH”
    nice and CREATIVE script 🙂

    thanks
    aman

  2. Fadi Dagher says:
    February 12, 2012 at 8:57 pm

    Pretty nice. A PAUSE in the end of the script would be in order 🙂

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