Introduction
Functions help keep your script neat and easy to understand. They let you reuse code without repeating it.
Functions help keep your script neat and easy to understand. They let you reuse code without repeating it.
function FunctionName { # Your code here } # To call the function: FunctionName
Functions start with the keyword function followed by the name and curly braces.
You call a function by writing its name.
function SayHello { Write-Output "Hello!" } SayHello
function Add-Numbers { param($a, $b) return ($a + $b) } $result = Add-Numbers 3 5 Write-Output $result
This script defines a function that greets a user by name. Then it calls the function with 'Alice'.
function GreetUser { param($name) Write-Output "Hello, $name! Welcome to the script." } # Call the function with a name GreetUser "Alice"
Functions make scripts easier to read and maintain.
You can pass information to functions using parameters.
Functions help avoid repeating the same code multiple times.
Functions organize scripts by grouping related code.
They make scripts easier to reuse and update.
Using functions helps keep your scripts clean and simple.
function Greet { Write-Output "Hi, $args[0]!" }
Greet "Alice"function ShowMessage {
Write-Output "Message: $message"
}
ShowMessage "Hello"Pass the test and this pattern moves unknown