Many repetitive tasks? Check Powershell function. [Post updated 2019-01-12]

Hi, This is Po(sh)land blog and now something amazing for beginners of Powershell. Now you can check how to write your own Powershell command. How do this? It’s possible with Powershell function. This is basic things which in the future help you write scripts and module. Check this out.

Powershell function – medicine for all evil

About PowerShell function, we can find much information, for example here, but for beginners, it can be more difficult so maybe this post will make thing easier. Function in Powershell is a group of steps what we can do step by step. For example, if you have morning routine,  you can write for this one Powershell function. We can run all morning steps via one command (function name). Function can be declare and check below how to do this.

function Start-MyFirstFunction { Write-Host "My first function run" }
powershell function


What did I write about? About the morning routine.

Ok, so check how it works for more than one steps. Check this out how it works.

function Start-MorningRoutine {
    Write-Host "Wake up" -ForegroundColor Gray
    Start-Sleep 3
    $ToDrink = Read-Host "Cafe or tea? "
    Write-Host "$ToDrink is being prepared, it's time go to bathroom"
    Start-Sleep 3
    Write-Host "$ToDrink has been prepared, drink and eat breakfast..."
    Write-Host "Go to work"
}
powershell function

Powershell function constructon

I want to show you the basic construction of the Powershell function. Before reading this paragraph, please look at the post “How to change the idea of working Powershell automation in 5 steps“. It helps you design Powershell function.

1. Function declaration  

First what you have to do is declare Powershell function. Please write function name in your code editor preceded by a “function” word.

2. Curly brackets

Now it’s time begin the sentence in curly brackets. It’s required to start to write tasks sequence what your function have to do.


3. It’s time for parameters.

Writing PS function is very similar to writing Powershell scripts. I wrote in the post 5 steps which help you to write your first Powershell script that one of the steps to write Powershell script is adding param block. More about param block you can find here Why Powershell param block will change your scripts to better.

You can add parameters to your function in two ways. The first, my recommended for beginners is to add param block in curly brackets.

In a second way, you can add parameters in round brackets after the function name.

4. Write your task sequence

Now you can write what function have to do. During this step, please fill out param block for all required parameters which should be available for the user during function invoking.

5. All function should return information

Now it’s time to end writing your function by declare returning data. What your function must return you should plan before you have started writing.

For better understanding see examples:

Function without defined return don’t give you result from your function. In the background function will sum numbers but don’t show you the sum.

powershell function

If you define return, function will show you the sum.

powershell function

You can also define result from function without ‘return’ keyword.

powershell function

Conclusion

When you do repetitive tasks in Powershell you can write one function to run this from one command. It’s very helpful and more productive than write all commands one by one. In this post I showed you basics of function but I would like the most people to understand the idea. Please write in comments you experience.

Get Access to Poshland Resources for FREE!!!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

    3 replies to "Many repetitive tasks? Check Powershell function."

Leave a Reply

Your email address will not be published.