Refactored Codes
Write your first basic barebone function in PowerShell.
Hey There, you can watch the live coding here on youtube. [embed]https://youtu.be/U0qd9um3ws4[/embed] Let us learn to write our first Powershell function. This is going to be a basic barebone function, this does not do much, just prints a string. We use function keyword to start definition of a function. Then we use start and end curly braces to define start and end of function code block. A very basic, bare bone function looks like this.
function Say-Hello {
    Write-Output "Hello";
}
  1. The function keyword tells the powershell that we are starting a definition of a function.
  2. Say-Hello  word provides the name of the function
  3. "Hello" is just a string literal
  4. Write-Output function prints the string "Hello"  to the terminal.
To run the function, just write the name and hit enter. Like shown in the screen shot, It is a screenshot of window command line, where Powershell Function is defined and executed, and function output is displayed. You can see in the screenshot, I have defined a function in the first line. In the second line, I am calling that function. Which results in the function output Hello. Try running the code yourself for better understanding. You can learn to pass parameters in a function - on here.. Thank you - Anurag Anand