How to Define A Subroutine In PowerShell?

8 minutes read

In PowerShell, a subroutine is called a function. Functions in PowerShell are defined with the function keyword. Here is the syntax to define a function in PowerShell:

1
2
3
4
function FunctionName {
    # Function body
    # Write your code here
}


To create a function, you start with the function keyword, followed by the name of the function (FunctionName in the example above). Inside the curly braces {}, you can write the code for your function.


Functions can also have input parameters. You can define input parameters by specifying their names inside parentheses after the function name:

1
2
3
4
function FunctionName (param1, param2) {
    # Function body
    # Write your code here
}


In this example, param1 and param2 are the input parameters of the function. You can use these parameters inside the function's body to perform specific actions.


After defining a function, you can call it by simply typing its name followed by parentheses ():

1
FunctionName


You can also pass arguments to the function when calling it:

1
FunctionName "argument1" "argument2"


Inside the function, you can use the return keyword to return a value. For example:

1
2
3
4
5
function FunctionName {
    # Function body
    # Write your code here
    return "Hello, World!"
}


This function will return the string "Hello, World!" when called.


By defining and using functions, you can create reusable blocks of code in PowerShell, enhancing the modularity and maintainability of your scripts.

Best PowerShell Books to Read in 2024

1
Mastering PowerShell Scripting: Automate and manage your environment using PowerShell 7.1, 4th Edition

Rating is 5 out of 5

Mastering PowerShell Scripting: Automate and manage your environment using PowerShell 7.1, 4th Edition

2
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

Rating is 4.9 out of 5

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

3
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Rating is 4.8 out of 5

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

4
PowerShell for Sysadmins: Workflow Automation Made Easy

Rating is 4.7 out of 5

PowerShell for Sysadmins: Workflow Automation Made Easy

5
PowerShell for Beginners: Learn PowerShell 7 Through Hands-On Mini Games

Rating is 4.6 out of 5

PowerShell for Beginners: Learn PowerShell 7 Through Hands-On Mini Games

6
Windows PowerShell Cookbook: The Complete Guide to Scripting Microsoft's Command Shell

Rating is 4.5 out of 5

Windows PowerShell Cookbook: The Complete Guide to Scripting Microsoft's Command Shell

7
PowerShell Pocket Reference: Portable Help for PowerShell Scripters

Rating is 4.4 out of 5

PowerShell Pocket Reference: Portable Help for PowerShell Scripters


How to pass by reference in a subroutine in PowerShell?

In PowerShell, you can pass arguments to a subroutine by reference using the [ref] type modifier. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Define a subroutine
function ModifyByReference {
    param (
        [ref]$refArgument
    )

    # Modify the value of the referenced argument
    $refArgument.Value = "Modified value"
}

# Define a variable
$value = "Original value"

# Pass the variable by reference to the subroutine
ModifyByReference -refArgument ([ref]$value)

# Check the modified value
$value


In the example above, the ModifyByReference subroutine takes a single argument $refArgument annotated with the [ref] type modifier. Inside the subroutine, you can access the value of the referenced argument using the .Value property. Any changes made to this property will be reflected in the original variable.


When calling the subroutine, you need to explicitly wrap the variable in the [ref] type modifier to indicate that you want to pass it by reference.


What is the meaning of "void" in relation to subroutines in PowerShell?

In PowerShell, "void" is used to describe a subroutine or function that does not return a value. When a subroutine is defined as void, it means that it performs a certain set of actions or operations but does not produce any output or result that can be used or stored.


In other words, a void subroutine is not assigned to a variable or used in an expression to retrieve a value. Its primary purpose is to execute a series of commands or tasks without returning any data.


For example, consider the following PowerShell function definition:

1
2
3
4
function DoSomething {
    Write-Host "Performing some actions"
    # code for performing actions goes here
}


The above function is void because it does not return any value. It executes the commands inside it but does not provide any result that can be assigned or used in subsequent operations.


What is the purpose of a subroutine in PowerShell?

The purpose of a subroutine in PowerShell is to group a set of instructions or script blocks together so that they can be called and executed multiple times within a script or program. Subroutines help in organizing and modularizing code, making it easier to read, understand, and maintain. They allow for code reuse and can be particularly useful when a specific set of instructions needs to be executed in different parts of a script or program. Subroutines also facilitate the passing of parameters or arguments, allowing for flexible and dynamic execution based on different input values.


What is the syntax for defining a subroutine in PowerShell?

The syntax for defining a subroutine (or function) in PowerShell is as follows:

1
2
3
4
function Function-Name {
    # Function body
    # Statements and code here
}


For example, to define a subroutine named "Say-Hello" that simply outputs "Hello, World!", you would use the following syntax:

1
2
3
function Say-Hello {
    Write-Host "Hello, World!"
}


You can then call this subroutine by simply typing its name (Say-Hello) in the PowerShell console or script.

Twitter LinkedIn Telegram Whatsapp

Related Posts:

To find the CPU and RAM usage using PowerShell, you can utilize various commands and methods. Here is how you can do it:Open PowerShell by pressing the Windows key, typing "PowerShell," and selecting "Windows PowerShell" or "PowerShell"...
PowerShell is a powerful scripting language and automation framework developed by Microsoft. It provides a command-line interface and scripting environment that allows users to automate tasks and manage computer systems. To install and configure PowerShell on ...
To create a shortcut using PowerShell, you can follow these steps:Open PowerShell: Launch PowerShell by searching for it in the Start menu or by pressing Windows + X and selecting "Windows PowerShell" or "Windows PowerShell (Admin)." Create the...