Skip to main content
TopMiniSite

Back to all posts

How to Fork New Powershell Window And Pass In Functions?

Published on
3 min read
How to Fork New Powershell Window And Pass In Functions? image

Best Tools for PowerShell Scripting to Buy in October 2025

1 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
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

BUY & SAVE
$47.49 $49.99
Save 5%
PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms
4 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$50.21
Learn PowerShell Scripting in a Month of Lunches
5 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$27.00 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
6 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
7 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.52 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
+
ONE MORE?

To fork a new PowerShell window and pass in functions, you can create a new PowerShell process using the Start-Process cmdlet with the -ArgumentList parameter. You can specify the function or script file as an argument in the -ArgumentList parameter.

For example, if you have a function called MyFunction in a script file named MyScript.ps1, you can fork a new PowerShell window and pass in the MyFunction by running the following command:

Start-Process powershell.exe -ArgumentList "-NoExit", "-Command", "&{ .\MyScript.ps1; MyFunction }"

This command will open a new PowerShell window, run the MyScript.ps1 script, and then call the MyFunction function. You can adjust the command to pass in multiple functions or scripts as needed.

Keep in mind that the script or function must be in the current working directory or you need to specify the full path to the script file for it to run successfully in the new PowerShell window.

How to pass in functions to a new PowerShell window when forking?

To pass functions to a new PowerShell window when forking, you can use the New-PSDrive cmdlet with the Persistent parameter to create a new drive that shares state with the current session. This allows you to pass functions to the new PowerShell window.

Here is an example of how you can do this:

  1. Define the function in your current PowerShell session:

function SayHello { Write-Output "Hello, World!" }

  1. Create a new drive that shares state with the current session and import the function:

New-PSDrive -Name "MyFunctions" -PSProvider "Variable" -Root $function:SayHello -Scope Global -Persist

  1. Open a new PowerShell window and access the function from the new drive:

& (Get-Item MyFunctions:\SayHello)

This will output "Hello, World!" in the new PowerShell window.

Remember to use the same steps to pass any additional functions or variables to the new PowerShell window.

How to fork a new PowerShell window using the Start-Process cmdlet?

To fork a new PowerShell window using the Start-Process cmdlet, you can use the following command:

Start-Process powershell.exe -WindowStyle Hidden

This command will create a new hidden PowerShell window. You can also specify other WindowStyle options, such as Normal, Minimized, or Maximized, to control the appearance of the new window.

What is the mechanism for encapsulating functions for secure transmission to a new PowerShell window?

To encapsulate functions for secure transmission to a new PowerShell window, you can use the ScriptBlock object in PowerShell. This allows you to define a block of code (function) that can be serialized and transported to another PowerShell session securely.

Here is an example of how you can encapsulate a function using a ScriptBlock object:

  1. Define the function that you want to encapsulate:

function MyFunction { Write-Host "Hello, World!" }

  1. Create a ScriptBlock object that contains the function:

$encapsulatedFunction = [ScriptBlock]::Create("function MyFunction { Write-Host 'Hello, World!' }")

  1. Serialize the ScriptBlock object and transmit it to a new PowerShell session:

$serializedFunction = $encapsulatedFunction.ToString() Invoke-Command -ScriptBlock { Invoke-Expression $using:serializedFunction }

By following these steps, you can encapsulate functions for secure transmission to a new PowerShell window using the ScriptBlock object. This helps ensure secure communication and execution of functions across different PowerShell sessions.