powershell-custom Functions (i) the first custom function

Source: Internet
Author: User

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

    1. Declare it as a function

    2. Test-function is our name for this custom function.

    3. Next () parentheses to define the parameters we need to use

      1. Define $name and $age two parameters

      2. We can assign an initial value to the parameter directly here.

    4. 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 "/>

    1. From the running results can be seen, we have $name and $age two parameters can be used, between the parameters with "," comma separated

    2. When we do not specify $age, we can see that it shows the default value of 18.

    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.