Skip to main content
freelanceshack.com

Back to all posts

How to Define A Subroutine In PowerShell?

Published on
4 min read
How to Define A Subroutine In PowerShell? image

Best PowerShell Subroutine Resources to Buy in October 2025

1 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

  • MASTER AUTOMATION: STREAMLINE YOUR SYSADMIN TASKS EFFORTLESSLY!
  • USER-FRIENDLY GUIDE: PERFECT FOR ALL SKILL LEVELS IN POWERSHELL.
  • DURABLE PAPERBACK: IDEAL FOR EASY READING AND NOTE-TAKING ANYWHERE!
BUY & SAVE
$23.99 $39.99
Save 40%
PowerShell for Sysadmins: Workflow Automation Made Easy
2 Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

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

BUY & SAVE
$39.99
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS
3 Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

BUY & SAVE
$26.62 $49.99
Save 47%
Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell
4 PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

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

BUY & SAVE
$49.49 $89.99
Save 45%
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell
5 PowerShell Pocket Reference: Portable Help for PowerShell Scripters

PowerShell Pocket Reference: Portable Help for PowerShell Scripters

BUY & SAVE
$18.99 $29.99
Save 37%
PowerShell Pocket Reference: Portable Help for PowerShell Scripters
6 PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers

BUY & SAVE
$32.60 $49.99
Save 35%
PowerShell Automation and Scripting for Cybersecurity: Hacking and defense for red and blue teamers
7 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
8 Windows PowerShell in Action

Windows PowerShell in Action

  • BRAND NEW: UNOPENED AND UNTOUCHED FOR GUARANTEED QUALITY.
  • COMPLETE PACKAGE: INCLUDES ALL ESSENTIAL ACCESSORIES FOR IMMEDIATE USE.
  • FAST SHIPPING: GET YOUR PRODUCT QUICKLY AND START ENJOYING IT SOONER!
BUY & SAVE
$59.99
Windows PowerShell in Action
9 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
+
ONE MORE?

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:

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:

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 ():

FunctionName

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

FunctionName "argument1" "argument2"

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

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.

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:

# 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:

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:

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:

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.