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.
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.
Hi “JH”
nice and CREATIVE script 🙂
thanks
aman
Pretty nice. A PAUSE in the end of the script would be in order 🙂