PowerTip: Add Custom Function to Runspace Pool

Summary: Boe Prox shows how to add a custom function to a runspace pool.

Hey, Scripting Guy! Question How can I use Windows PowerShell to add a custom function to a runspace pool?

Hey, Scripting Guy! Answer Use the following approach:

#Custom Function

Function ConvertTo-Hex {


    ‘0x{0:x}’ -f $Number


#Get body of function

$Definition = Get-Content Function:\ConvertTo-Hex -ErrorAction Stop

#Create a sessionstate function entry

$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry
-ArgumentList ‘ConvertTo-Hex’, $Definition

#Create a SessionStateFunction


 #Create the runspacepool by adding the sessionstate with the custom function

$RunspacePool = [runspacefactory]::CreateRunspacePool(1,5,$InitialSessionState,$Host)

Comments (0)

Skip to main content