In PowerShell, we can customize a function to implement a specific function to achieve the purpose of reuse. Prepare to share it with a small series from simple to complex.
First, we write a very simple custom function.
Goal:
After entering the name and age, directly in the show how old this year.
Example:
Function test-function ($Name, $Age = "18")
{
Write-host "$Name $Age year old."
}
Description
Declare it as a function
Test-function is our name for this custom function.
Next () parentheses to define the parameters we need to use
Define $name and $age two parameters
We can assign an initial value to the parameter directly here.
Then write the code block in {} to implement the functionality we need.
Operation Result:
650) this.width=650; "src=" https://s5.51cto.com/wyfs02/M02/90/BB/wKioL1jx9g-hSgdgAABEdQZ-zLY202.jpg "title=" 1.jpg "alt=" Wkiol1jx9g-hsgdgaabedqz-zly202.jpg "/>
From the running results can be seen, we have $name and $age two parameters can be used, between the parameters with "," comma separated
When we do not specify $age, we can see that it shows the default value of 18.
We can also re-assign the $age, and the result changes after you reassign it to 20.
Custom functions are a great way to reuse a piece of code. Of course, custom functions also include more and more complex content. It will be updated slowly.
This article from "Puma pot" blog, declined reprint!
powershell-custom Functions (i) the first custom function