Skip to content
mnaoumov.dev
Go back

PowerShell scripts and "Pattern Main"

Usually when I write PowerShell scripts I need to introduce a helper function

Script.ps1

for ($i = 0; $i -lt 10; $i++)
{
    Write-Host "$i * $i = $($i * $i)"
}

We want to extract a separate function

function Get-Square($n) { return $n * $n }

for ($i = 0; $i -lt 10; $i++)
{
    Write-Host "$i * $i = $(Get-Square $i)"
}

As you see, we had to declare helper function before we can use it. And when we introduce more and more functions we need to declare them before their usage.

I think this is very inconvenient. Your script file will eventually look like

function HelperFunction1
{
...
}

function HelperFunction2
{
...
}

...

function HelperFunction20
{
...
}

# Only here core script itself that uses all the functions

Usually when you read your script you are interested in your core script logic more than helper functions so it is more logical to have it at the top of the script

To have it, I invented a very simple pattern “Main”. You just need to wrap you core logic into a Main function at the beginning of the script and then call Main function at the end of the script.

function Main
{
    # Put our core script logic here
}

function HelperFunction1
{
...
}

function HelperFunction2
{
...
}

...

function HelperFunction20
{
...
}

Main

Now it is easier to read and still a valid script.

Pretty simple, isn’t it?


Share this post on:

Previous Post
C__PowerShell Clipboard Watcher
Next Post
Efficient Base64 conversion in PowerShell