In this PowerShell, we can create and use functions that can accept objects as parameters. In this PowerShell tutorial, I will explain how to pass objects to functions in PowerShell with examples.
In PowerShell, you can pass objects to functions by defining parameters in the function that expect an object type. To pass an object, you simply call the function and specify the object as an argument. For example, if you have a function Process-Document
that expects a document object, you can call it with Process-Document -Document $myDocument
, where $myDocument
is an object representing the document you want to process.
How to Pass Objects to Functions in PowerShell
In PowerShell, everything is an object. An object is a structured data that is represented by properties and methods. For example, when you get a list of files in a directory using the Get-ChildItem
cmdlet, each file is represented as an object with properties like name, size, and creation date.
A function in PowerShell is a block of code designed to perform a particular task. Functions help you to encapsulate logic that you can reuse in different parts of your scripts. Parameters allow you to pass values or objects into a function, which the function can then use internally.
Basic PowerShell Function Syntax
Here’s a simple example of a function that accepts a parameter in PowerShell:
function Greet-User {
param (
[string]$name
)
Write-Output "Hello, $name!"
}
You would call this function and pass a string to it like this:
Greet-User -name "Alice"
Passing Objects to Functions in PowerShell
Passing objects to PowerShell functions is not much different from passing simple data types like strings or integers. Let’s say we have a function that needs to process a file object.
Example 1: A Function Accepting a File Object
function Get-FileInfo {
param (
[System.IO.FileInfo]$file
)
Write-Output "Processing file: $($file.Name)"
Write-Output "Size: $($file.Length) bytes"
}
You can call this function with a file object like this:
$fileObject = Get-Item "C:\MyFolder\MyNewFile.txt"
Get-FileInfo -file $fileObject
In this example, Get-Item
retrieves the file object, which is then passed to the Get-FileInfo
function. The function uses the .Name
and .Length
properties of the file object.
Here is the complete PowerShell script.
function Get-FileInfo {
param (
[System.IO.FileInfo]$file
)
Write-Output "Processing file: $($file.Name)"
Write-Output "Size: $($file.Length) bytes"
}
$fileObject = Get-Item "C:\MyFolder\MyNewFile.txt"
Get-FileInfo -file $fileObject
You can see the output after I ran the script using VS code.
Example 2: A Function Accepting Multiple Objects
Sometimes, you might need to pass more than one object to a function. Here’s how you can do that:
function Compare-Files {
param (
[System.IO.FileInfo]$firstFile,
[System.IO.FileInfo]$secondFile
)
if ($firstFile.Length -eq $secondFile.Length) {
Write-Output "Files are of equal size."
} else {
Write-Output "Files are of different sizes."
}
}
# Usage
$fileOne = Get-Item "C:\file1.txt"
$fileTwo = Get-Item "C:\file2.txt"
Compare-Files -firstFile $fileOne -secondFile $fileTwo
PowerShell also supports advanced parameter features like parameter validation, default values, and pipeline input. For instance, you can define a function that accepts pipeline input, which is very useful when you want to process a series of objects.
Example 3: A Function That Accepts Pipeline Input
function Get-LargeFiles {
param (
[Parameter(ValueFromPipeline=$true)]
[System.IO.FileInfo]$file
)
process {
if ($file.Length -gt 10MB) {
Write-Output $file
}
}
}
# Usage
Get-ChildItem "C:\someFolder" -Recurse | Get-LargeFiles
In this example, Get-ChildItem
retrieves all the files in a specified directory and passes them through the pipeline to Get-LargeFiles
, which outputs only the files larger than 10MB.
Conclusion
In this PowerShell tutorial, I have explained various examples of how to pass objects to functions in PowerShell.
You may also like:
- PowerShell Function Return Values
- How to Exit a Function Without Stopping the Script in PowerShell?
- PowerShell Function Array Parameters
- How to Call Function in If Statement in PowerShell?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com