Challenge

A Beginner PowerShell Function Challenge

thermometer-thumbnailThe Chairman realizes that many IT Pros who take on his challenges are experienced and advanced PowerShell users. But everyone was a beginner once. Everyone had that first challenge of writing a PowerShell function. Today’s challenge is for those of you in the early stages of learning PowerShell. Now, it is time for you to begin scripting. For this beginner challenge, the Chairman would like you to write 2 very simple functions.

The Challenge

You should write a function to convert a Fahrenheit temperate to Celsius and a second function to do the opposite. The functions can be very simple with a single parameter for the value to convert. You don’t need to worry about passing values from the pipeline, error handling or anything advanced. The function can simply write the converted value to the pipeline as a result. If you are feeling a bit more confident, have your function write an object to the pipeline with the original and converted values.

Even though this is a basic and beginner challenge, you should still follow PowerShell scripting best practices such as using a proper verb-noun naming convention. If you feel a bit more skilled, feel free to include error handling, parameter validation and pipeline input.

Converting Help

To help you along, you can use these formulas to convert temperatures. To convert from Celsius to Fahrenheit: (C*1.8)+32. To convert from Fahrenheit to Celsius is a matter of reversing the process: (F-32)/1.8. As points of reference, freezing is 32F and 0C. Boiling is 212F and 100C.

As always, please don’t submit any code solutions as comments. But you are encouraged to share links to your work.

4 thoughts on “A Beginner PowerShell Function Challenge”

  1. This challenge triggered a trip down memory lane as I vaguely recall writing a similar conversion program, probably in Commodore Basic, during the ’80s.

    I provided simple, intermediate, and advanced functions. The advanced functions includes a parameter to assist the user with decimal precision.

    Just for the learning chance, I also provided a Temperature custom class. The gist has a nice table of the class methods.

    https://gist.github.com/thedavecarroll/458429ec442b109ea8d6ba46b9ed7b18

    Please feel free to post any comments on the gist. Sample output from all functions and some class methods are posted as a comment to the gist.

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *