Why Powershell param block will change your scripts to better

Hi, this is Poshland blog about basics Powershell for everyone. 
Last on Reddit forum I reply for one person how to insert variable to script without script editing.  Today I thought that it can be subject to next post. So Let’s check what is param block.  



Remember what is variable 

Few posts ago I write My Way to understand Powershell variable. It shows you what is it variable. One of the next posts was about Powershell function which is important to this post. Please read and come back ?

You don’t have to edit script to change variable. Use Powershell param block.

In the past, a long time ago when I started with Powershell, I was writing very basics script. My scripts were very basics because I wasn’t using functions, param etc. Whenever I wanted to use my old Powershell script, I had to edit it to change a variable.

Now I know Powershell much better and I will share with you the basics of param block. OK, so let’s check how it works.

powershell param block
param (
    [string]$path,
    [string]$Type
)

Write-Host "This is my $path"
Write-Host "This is My $Type"

Write-Host "So I can use $path and $Type inserted during script start"

Powershell function – better way to write the script

When you write a few line script to do something I think the better way is to write Powershell function, because you can use it in the next script if you needed. I don’t write all information about Powershell function because there is enough information to write a separate post. I will show you only the basics of use param block in Powershell function. Check this out.

powershell function param block
function Start-FunctionWithParam {
    param (
        [string]$path,
        [string]$Type
    )
    
    Write-Host "This is my $path"
    Write-Host "This is My $Type"
    
    Write-Host "So I can use $path and $Type inserted during script start"
}
Start-FunctionWithParam -path MyNewPath -Type $MyNewtype

Script and functions – how to use them together 

When we know how to use param block in Powershell script and Powershell function, it’s time to check how it works together. Below I show Powershell script with function.  I think that example will be the better description.

powershell param block
param (
    [string]$Path1,
    [string]$Type1
)

function Start-FunctionWithParam {
    param (
        [string]$path,
        [string]$Type
    )
    
    Write-Host "This is my $path"
    Write-Host "This is My $Type"
    
    Write-Host "So I can use $path and $Type inserted during script start"
}

Start-FunctionWithParam -path $path1 -Type $Type1

Conclusion

I want to tell you about one conclusion. Write your Powershell scripts and functions, so that you can use them again in the next scripts. Write scripts always universally.

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 MailerLite ( more information )

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