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";
}
- The function keyword tells the powershell that we are starting a definition of a function.
- Say-Hello word provides the name of the function
- "Hello" is just a string literal
- 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,
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