I've been experimenting with PowerShell and created two scripts that deal with converting strings to and from Base64 and Hex formats. I'm trying to grasp the use of ParameterSets and I noticed there's no built-in cmdlet for converting to and from these formats, so I decided to write my own functions. I have included both scripts below and I'm looking for some feedback. What are your thoughts on best practices, optimizations, or any advice you can offer?
6 Answers
Using .NET directly feels more straightforward to me for basic conversions like these, rather than creating extra functions. Since .NET is already available in PowerShell, I don’t mind calling those methods directly if it keeps the script simpler.
When it comes to using parameter sets, it’s usually better to use a specific parameter to identify which set is in use. It's fine to have a default, but be cautious with them. They should be used sparingly; ask yourself if it's truly necessary. Additionally, your parameter set identity should remain internal—try to avoid mixing that logic into user outputs directly.
Consider adding documentation within your function to explain that the value will be encoded in UTF8. You could include this in the .NOTES or .PARAMETER section of your help documentation—it just helps users understand what's happening.
I usually don't create regions for every function—maybe just one for all functions when necessary. Keeping things sparse helps in readability, especially if the script is already long. Just a thought!
Hey! I think your scripts have a good starting point, but there are a few areas where you could simplify things. For instance, the verbose output could be streamlined; you might want to consider using simpler error handling instead of relying too heavily on built-in validation. Also, instead of breaking your functions down into regions, it might be cleaner just to use braces for the functions themselves. Here's a cleaner version that removes some of those extra pieces like the verbose statements unless necessary!
I see what you mean! Using a default value with breaks in the switch prevents it from getting stuck, right? I totally agree with you about the verbose output, I tended to use it for debugging too, but here strings can definitely help organize long outputs better.
There's actually a module called 'convert' on PSGallery that has a lot of those conversion cmdlets you're looking for! It has about 35 cmdlets, including one for Base64, though not specifically for Hex yet. If you're open to using modules, that could save you some time.

I actually got the idea to try this from one of your videos! Your explanations really help make these concepts clearer.