Making a Paint Calculator in Powershell

First of all, open up a new Powershell ISE window. You’ll also want to make sure that your script execution policy allows for scripts to be run. To do this you can follow this guide.

Now that that’s out of the way, lets start creating our script.

To start off, we want to collect some of basic values. These values will be put into a variable. In the below screenshot you can see that I have collected some basic information using the “Read-Host” option.

1

Now that we have these I’m going to get the total area of the wall and put it into the variable “$wall”. I now divide the wall area by the spread of the paint and make this round up to the nearest integer.

2

Now I get the price of a tin of paint and times it by the rounded up variable just discussed.

3

Now we get onto the actual function. We start off by taking the rounded up amount of tins and saying “if it is less than one,” then simply output the original price the user inputted as only one tin of paint is needed. Else, we say “write the amount of tins needed and the total price”

4

Here is the full script with descriptors:

============================================================================

function paint {

#Get and then times height and length. Then get the spread of the tin of paint. For example 80m.
[int]$length = Read-Host 'length of walls in meters?'
[int]$height = Read-Host 'height of walls in meters?'
[int]$wall = $height * $length
[int]$spread = Read-Host 'Coverage of a single tin of paint in meters (not meters squared)?'

#now we take the total area and divide by the coverage of a tin of paint
[int]$add = ($wall / $spread)

#now we take this and round it up to the nearest integer (this gets the amount of tins needed)
[int]$div = [math]::Ceiling($add)

#now we get the price of a tin of paint
[int]$price = Read-Host -Prompt 'how much does a tin of paint cost?'

#Now we times the inputted price by the rounded up amount of tins needed
[int]$tprice = $price * $div

#now we do the function
#if the amount of tins is less than 1, then output original inputted price
#if larger than one, ouput the amount of tins needed and total price.
if ( ($div -le [int]1) ) {
Write-Host "Less than one tin is needed. This will only cost £$price"
}else {
Write-Host "$div tins of paint are needed. This will cost £$tprice"
}
} paint

pause

============================================================================

I added the “pause” at the end since I didn’t want the Powershell box to disappear as soon as all the variables were collected.

Hope you enjoyed and found this useful, if not interesting.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s