Functions
A function is a set of statements that are executed when the function name is called. Functions allow programmers to break down a problem into smaller chunks and help define a particular task. A couple of the major reasons for using functions are readability and reusability.
Function Definition
The syntax for defining functions in PowerShell is:
Function <Name>{
<statement 1>
<statement 2>
<statement 3>
...
}
The Name
of the function is used to invoke the function. Statements are written between the curly brackets { }
.
Invoking a Function
To invoke or call a function, simply use the function’s name.
Function StatusDisplay{$date = get-date -format "MM/dd/yyyy HH:mm:ss"write-host "Script is running at $date"}StatusDisplay# Output: Script is running at 02/27/2023 06:24:04
Parameters and Arguments
Parameters can be added to the function declaration to make them more flexible. The keyword param
is used to declare all parameters in the first function statement.
Function <Name>{
param (<parameter 1>, <parameter 2>, ...)
<statements>
...
}
To invoke a function with arguments, the values passed to the function as parameters, list arguments after the function call:
<Name> <argument 1> <argument 2> ...
Example
Function StatusDisplay{param ($filename, $status)$date = get-date -format "MM/dd/yyyy HH:mm:ss"write-host "$filename is running $status at $date"}StatusDisplay "server.ps1" "normal"# Output: server.ps1 is running normal at 02/27/2023 22:57:29
Default Parameters
A function will use default parameters unless arguments are explicitly provided in the function call. Default parameters are defined by assigning values to parameters:
Function <Name>{
param (<parameter 1> = <value 1>, <parameter 2> = <value 2>, ...)
<statements>
...
}
Example
Function StatusDisplay{param ($filename, $status = "normal")$date = get-date -format "MM/dd/yyyy HH:mm:ss"write-host "$filename is running $status at $date"}StatusDisplay "server.ps1"# Output: server.ps1 is running normal at 02/27/2023 22:57:29
Importing Functions
Functions (as well as variables, cmdlets, and more) can be imported from another script .ps1
file using the Import-Module
cmdlet. Its syntax is:
Import-Module <Path to file containing functions>
Example
Assume there’s a function called StatusDisplay
in a script file called my_functions.ps1
. All modules including the StatusDisplay
function can be imported into another script like so:
Import-Module .\my_functions.ps1StatusDisplay# Output: Script is running at 02/28/2023 12:29:16
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PowerShell on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn PowerShell
Jump into PowerShell through interactive lessons on variables, operators, control flow, objects, arrays, and functions.Beginner Friendly6 hours