Strings, Arrays and Functions in PowerShell v2 (and some sample code that speaks for itself :-)

I spent some more time experimenting with PowerShell v2 and here goes my second post about it.

This time around I am focusing on how to define variables, use expressions and create functions.

The "problem" we're solving :-)

To make it fun, I decided to create a little script that creates some random syllables, words and sentences using a set of rules. This will come out as some nonsense sentences, but they should be pronounceable. I wrote this little program in several languages before and it generally exercises a number of concepts around manipulating arrays and strings. It uses random numbers and sets of consonants and vowels, plus some logic around the number of syllables per word, words per sentence, etc.

Here are a few sample sentences created by the program:

Inepirula beicmicso phosa.

Ci muesit sacoipae naepdave opeefane.

Kikipor sofestrysu crasha browohocyn.

De dyczershaco kurensataer mexi trerememee.

Cie he cho cyuba lio goabryntune.

Quiaphe breiepco henureme sopoche.

Pychurmabie osipi leberoi.

Ne trirorce auencuep je sucootoi yeelneer.

Uchekepian rinsi reypbar.

Oeciurgo vacra go.

It’s gibberish, I know, but you should be able to pronounce it. In fact, I went one step further and used the Speech API in Windows to actually have the computer say them out loud.

P.S.: Due to its random nature, it is possible (although somewhat unlikely) that the program might spit out some bad words in some real language. I apologize in advance :-)

What I learned

PowerShell proved quite capable of creating this program. In fact, due to its interesting abilities to turn strings into arrays, rich expressions and loop constructs, it was no problem at all.

I did learn a few details that always vary from language to language:

·PowerShell uses a $ sign before the name of variables

·When creating variables, you can specify the type in [], but that is optional

·Common types include int32, array, string and object

·Get-Random(n) will give you a random integer between 0 and (n-1).

·Strings are arrays of characters starting at item number 0

·The -split operator can turn a string into an array, splitting the string using a specified delimiter

·Substring is the function to get a piece of a string

·Length is the function to get the number of characters in a string (or items in an array)

·Some operators are a bit tricky and it doesn’t use the regular "<", ">", "<=", ">=", "!=" and "==" operators. You need to use "-lt", "-gt", "-le", "-ge", "-ne" and "-eq" instead (there are many more). You get used to it.

·You can use the “+=”, “-=”, “*=” assignment syntax as you have in C++, C#

·You will sometimes need to enclose an expression in ( ) so that it is not confused with a statement

·You can use the multiply operator to repeat a string. For instance (“-“ * 5) yields “-----“. This is one of those cases where you must use the ().

·In loops and conditional statements, you use () to specify the conditions and { } to group the statements

·The default execution policy will not let you run an unsigned script. You can bypass this using “Set-ExecutionPolicy Unrestricted -scope process”. Note that this will only apply for the running process, due to the –scope parameter. You can set this at a wider scope, but you probably shouldn’t, for security reasons.

Closing comments

Last but not least, find the code below. I tested this on Windows Server 2008 R2. Try it only on a test machine.

You should probably save it in a file called sentences.ps1 (for instance) and run it from there using “.\sentences.ps1”.

Consider using the Powershell ISE, which includes a debugger.

Use CTRL-C to stop it before it says all 10 sentences. You might have to wait for it to end saying a sentence before it actually stops.

Now that you got this code, you might want to work on it and make it better. Maybe changing the frequency of the consonants, vowels and endings. Maybe tweak the number of syllables per word or words per sentence. You might even think of ways to make it generate some poetry with the right number of syllables per sentence and some rhyming. Have fun…