Microsoft Scripting Guy Ed Wilson here. The cool thing about scripting on the weekend is you can take two hours for breakfast, hang out with friends on Facebook and Twitter, and not feel bad about it all. The Scripting Wife headed out with her friend to a rodeo and left me in the kitchen unattended. Bad mistake on her part!

I got into the smoked salmon and steel-cut oats, made Kona coffee in my French press, and boiled three eggs. I rummaged around in the refrigerator and found some organic blueberries for the oatmeal, and put two cinnamon sticks into the food processor so I would have fresh ground cinnamon to put on top. After letting the Salmon decant for a half hour, I got into the capers, chopped a red onion, and sprinkled some lemon grass on top. After letting the eggs steep for 25 minutes, it was time to clean them, pat them dry, and sprinkle some fresh ground pepper on top. Needless to say, I completely filled up the dish washer and it was nearly time for lunch by the time I finished breakfast. Hey, what are weekends for, if not for scripting and cooking?

Because I know the Scripting Wife will probably not be as enthusiastic about my kitchen activities as I am, I am going to write her a function she has been asking for: the calculate fuel cost function. She has been asking for this function since we took our recent trip to Chattanooga, Tennessee; here’s a photo from that trip.

Here’s the script.

Get-FuelCostFunction.ps1

Function Get-FuelCost
{
Param(
\$miles,
\$milesPerGallon = 23,
\$costPerGallon = 2.75,
[switch]\$RoundTrip,
[switch]\$costPerMile,
[switch]\$irsRate
)#end param
If(\$RoundTrip)
{
if(\$irsRate)
{
(\$miles * 2) * .55
exit
}
Else
{
"{0:n2}" -f (((\$miles * 2) / \$milesPerGallon) * \$costPerGallon)
exit
}
}
If(\$costPerMile)
{
if(\$irsRate)
{
".55"
exit
}
else
{
"{0:n2}" -f (\$costPerGallon / \$milesPerGallon)
exit
}
}
If(\$irsRate)
{
\$miles * .55
}
else
{
"{0:n2}" -f ((\$miles / \$milesPerGallon) * \$costPerGallon)
}
}#end Get-FuelCost

The Get-FuelCost function seen above can be used to calculate a one-way trip, a round trip, or a simple cost-per-mile calculation. In addition, the trip calculations can utilize the 2009 IRS mileage guidance of .55 cents per mile if you wish to have an idea of other costs besides simple cost of the fuel.

As seen in the following image, if you were to drive one way on a trip that is 200 miles long, and the cost per gallon was \$2.90 and your vehicle got only 12 miles per gallon, your fuel cost for the trip would be \$48.66.

If you are taking a round trip, and the cost per gallon of gasoline is \$3.12 and your vehicle gets 26 miles to the gallon, your fuel cost will be \$48.00. The command line is seen here:

Get-FuelCost -miles 200 -costPerGallon 3.12 -milesPerGallon 26 -RoundTrip

As the function is currently written, you can simply open the script in the Windows PowerShell ISE, and modify the command, as you wish. There are two default values, which are the two things that are most common: the milesPerGallon value and the costPerGallon value. You can edit the values in the param section of the function to supply defaults that are more in keeping with your environment. Using the function is easier when using the default values. From Charlotte, North Carolina, to Chattanooga, Tennessee, is 348 miles according to Bing. Using the default values of 23 miles per gallon and 2.75 per gallon for gas, the Get-FuelCost function tells us it will cost \$83.22 for a round trip. The command line is seen here:

Get-FuelCost -miles 348 -RoundTrip

If you do not want to open the function to edit it each time you wish to use it, you can copy and paste the function into your profile, add it to a module, or dot-source the script into your current Windows PowerShell console.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson and Craig Liebendorfer, Scripting Guys