Use PowerShell To Test Code Snippets

If you are like me, then you might have multiple Console applications in your projects folder that you use for testing small chunks of code.  I think at one point recently, I was up to ConsoleApplication28 before I cleaned up the unused projects.  These are typically used for testing small snippets of code that I am planning to use in other projects that actually have meaningful names, but I just want to play with the small snippet rather than deal with running through the complete project to test my one or two line experiments.

Thankfully,  I have recently started using PowerShell to experiment with snippets and this has proven to be much easier.  I will run through a few common scenarios and show how to user PowerShell to test these snippets.


Testing Regex Expressions

How many times have you had to manipulate a Regex before getting it right?  It can be very annoying if you have to keep rebuilding your project to test these match expressions.  Well, here is a simple way to do this in PowerShell:


In PowerShell, we can use .NET Framework libraries just as we do in C#.  The syntax is slightly different, but in this case we are just calling the Regex.Matches method.  After executing the command, you can see the groups that were matched, as well as some of the other properties associated with calling the Matches method.


Testing DateTime Formatting

Another common experiment is to test the returns of DateTime formats.  Here are a few examples of how to do this in PowerShell:


The above example shows a few common DateTime tasks that can be easily done in PowerShell.



There are a lot more really cool features of PowerShell that can really help when you need to test code samples.  I will try to post some more about this in the near future.  Until then, play around with is a really powerful tool.