Quick tip: Dynamically create and use variables

Creating and using variables in PowerShell is simple. Unlike most other languages you don’t need to initialize a variable before use – you don’t even need to type them; PowerShell will attempt to set the type based on the value you give it. Because they are so simple to use, it’s easy to forget that we actually have a set of cmdlets for working with variables. (Get/Set/New/Clear/Remove-Variable) But that’s ok, they are seldom needed. But in this quick tip I will show you how to create and use variables dynamically, and then they come in handy indeed. (more…)

Quick tip: Enum with string values

Using enums can come in handy in many situations. When writing an advanced function in PowerShell, using an enum as the parameter type will ensure that only valid values are used, as well as give you tab-complete functionality (The same can be achieved using ValidateSet).

Now, I was working on extending my Mockaroo function when I found myself in a position where I needed to validate a large amount of values for a parameter. I decided on an enum, but I had problem – some of the values where strings with spaces in them, and I was going to use the parameter value when creating a URI for the Mockaroo API, so I’d rather avoid using a huge switch-statement translating the enum values to strings.
(more…)