Bootstrap FreeKB - PowerShell - Getting Started with functions
PowerShell - Getting Started with functions

Updated:   |  PowerShell articles

A function is a collection of reusable markup. In this example, there is a subroutine named "foo" that will print "Hello World";

function greeting {
  Write-Host "Hello World"
}

 

Or like this.

function greeting {
  $hello = "Hello World"
}

 

In this example, the following markup will invoke the greeting, which will print "Hello World".

greeting


Often, functions have a return.

function greeting {
  return "Hello World"
}

 

In this example, the string returned in the greeting function is stored in the $hello variable.

$hello = greeting
Write-Host $hello

 

Often, values are passed into a function.

function greeting($first, $last) {
  return "Hello $first $last"
}

 

In this example, "Hello John Doe" should be printed.

$hello = greeting 'John' 'Doe'
Write-Host $hello

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 428728 in the box below so that we can be sure you are a human.