Tag Archives: Scripting

Friday Fun: Pick Up Milk

sticky-graphic Sometimes when working on a PowerShell problem, you might have to come to the conclusion that PowerShell is not the right tool for the job. There are some tasks or applications that simply don’t lend themselves to automation or PowerShell. This isn’t necessarily a limitation in PowerShell, nor would I say it is a deficiency in the application at hand. It is just the nature of IT.

That said, there may be situations where you still want to use PowerShell, even if the experience is less than optimal. So let’s have some fun with this today. For a number of versions of Windows client’s there has been a little utility called Sticky Notes.

This is an electronic version of Post-It Notes. Wouldn’t it be nice to be able to use PowerShell to take advantage of the utility? Sadly, this application is written in such a way that nothing is exposed that can be automated, either through COM or .NET. So we’re left with a drastic measure of sending keys to the application.

As in the days of VBScript, sending keys is a risky proposition because you can’t always guarantee that the application in question will have focus. But let’s try. First, we need to start the program simply by invoking the command stikynot.

To send keys I’m going to use the .NET Framework. So to be on the safe side, I’m going to add some necessary assemblies just in case. It won’t matter if they are already loaded.

Now to activate the window. You can do this by the window title.

Or the process ID. I’ll use the title.

While the window has focus, I can send keys to it.

And that’s all there is to it! Sticky Notes have a number of keyboard shortcuts you can send using this technique. I found a page at http://www.door2windows.com/list-of-all-keyboard-shortcuts-for-sticky-notes-in-windows-7/ that listed many of them.

But you know I’m not going to stop here. I built a module called StickyNotes.

The module includes several functions for creating, adding, setting and removing sticky notes. The tricky part was getting the timing right. You might need to adjust some sleep values, or be prepared to re-run a command. I would not build any mission critical processes around anything that uses send keys techniques as it isn’t reliable. But it sure is fun!

You might even use it like this:

At the end of the command, a sticky note pops up letting you know the deed is done. You might see an error about a missing process, but you can ignore it. Or maybe you simply need a reminder to pickup that loaf of bread or your child on the way home.

Sometimes PowerShell isn’t the best tool for the job, but I think it can always be fun. Let me know what you think of my little project.

Have a great weekend.

Sorting Hash Tables

letterjumbleOver the weekend I received a nice comment from a reader who came across an old post of mine on turning an object into a hash table.  He wanted to add a comment but my blog closes comments after a period of time. But I thought it was worth sharing, especially for those of you still getting started with PowerShell. The comment was on how to sort a hash table.

Let’s say you have a simple hash table like this:

And is displayed like this:

To sort on the keys, you can use the GetEnumerator() method which is part of every hash table object. This method creates a System.Collections.DictionaryEntry object for each item in the hash table.

This means you can sort on any property.

By the way, starting in PowerShell 3.0, you could “pre-sort” the hash table by defining it as ‘ordered’.

Now the hash table will always be sorted in the order you defined the entries.

Although, if you want to sort you still can.

Have a great week and I hope you get things sorted out.

Friday Fun – Does Anyone Really Know What Day It Is?

eternalclock_150x150 This week’s Friday Fun I think epitomizes how much I think about PowerShell. But I also think it will serve as a useful learning device if not something you might actually want to use. For some reason, I thought it would be useful to quickly display a monthly calendar in PowerShell. Sure, I could click over to the clock on the start bar and navigate to a calendar. But then I have to take my hands off the keyboard and click around with a mouse. I’d much rather type a quick command, maybe give it a date and see the results. So this is what I came up with: Get-Calendar.

The script will define an alias, gcal, for the function.

Because calendars are culture (i.e. language) specific, I wanted to write something that anyone could use, and not something just for North American users. That meant retrieving culture settings so I could use language specific versions of days of the week. I think. It is very hard to fully test this without a system running completely in a different culture.

I wanted my calendar to display abbreviated week day names in the correct culture.

After quite a bit of experimenting, I realized the best way to create the calendar was to write each line as a separate string. This meant one line would be the abbreviated week day names.

Then each week would be a string of numbers. But this was the tricky part. It was easy enough to find out how many days were in the month, and what the first day of the month would be.

Here’s the challenge: I can figure out that the first day of the month might be on a Thursday. Which means I need to align ‘1’ under the THU heading, and then add enough other days until I reach the end of the week.

So I created an array, $Week1, to hold a set of values. I use a FOR loop to check if the date’s short day name matches the array of abbreviated day names from the UI culture, I know that is the first day of the month. Otherwise, I simply add a 2 space “filler” to the array. Then I need to add the rest of the days for the first week.

With that, I can begin constructing a text representation of the calendar using a here string.

The clever part is using the -Join operator to essentially concatenate all of the values in the week 1 array, separating each by a space. From here, I repeat the process for each week until I reach the end of the month.

At the end of the process $mycal is a here string with the calendar. I could simply write $mycal to the pipeline and it would look just fine. However, because you might want to save the calendar to a text file, I found it best to split the here string back into an array.

Now you can create a text file calendar for next year:

Or run the command at the prompt.

The function defaults to the current month or you can specify any date. You can even write it to the host and add a little color.


If you run PowerShell under something other than en-US, I’d love to hear how this works for you. Hope you have a little bit of fun with this.