Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3316070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:25:33+00:00 2026-05-17T22:25:33+00:00

I am trying to figure out how ScriptBlock.GetNewClosure() works. Based on this thread (see

  • 0

I am trying to figure out how ScriptBlock.GetNewClosure() works. Based on this thread (see Stej’s answer) I have this code:

$i = 1
$block1 = 
{
    $i
}

$i = 2
$block2 = 
{
    $i
}

$i = 3
$block3 = 
{
    $i
}


& $block1
& $block2
& $block3

The output is:

3
3
3

which is expected, because when the ScriptBlocks are executed the current $i value is used. This can be changed using GetNewClosure():

$i = 1
$block1 = 
{
    $i
}.GetNewClosure()

$i = 2
$block2 = 
{
    $i
}.GetNewClosure()

$i = 3
$block3 = 
{
    $i
}.GetNewClosure()


& $block1
& $block2
& $block3

This time the output is:

1
2
3

This is good, but when I try to star the ScriptBlocks as jobs:

$i = 1
$block1 = 
{
    $i
}.GetNewClosure()

$i = 2
$block2 = 
{
    $i
}.GetNewClosure()

$i = 3
$block3 = 
{
    $i
}.GetNewClosure()


$job1 = Start-Job $block1
$job1 | Wait-Job 
$job1 | Receive-Job

$job2 = Start-Job $block2
$job2 | Wait-Job 
$job2 | Receive-Job

$job3 = Start-Job $block3
$job3 | Wait-Job 
$job3 | Receive-Job

There is no output. Searching for an answer, I found this thread, where x0n says that jobs are executed in a dynamic module. Modules have isolated sessionstate, and share access to globals. PowerShell closures only work within the same sessionstate / scope chain

Does this mean that $i is not accessible from jobs? When I test its value:

$i = 1
$block1 = 
{
    $i -eq $null
}.GetNewClosure()

$i = 2
$block2 = 
{
    $i -eq $null
}.GetNewClosure()

$i = 3
$block3 = 
{
    $i -eq $null
}.GetNewClosure()


$job1 = Start-Job $block1
$job1 | Wait-Job 
$job1 | Receive-Job

$job2 = Start-Job $block2
$job2 | Wait-Job 
$job2 | Receive-Job

$job3 = Start-Job $block3
$job3 | Wait-Job 
$job3 | Receive-Job

$ is equal to null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:25:33+00:00Added an answer on May 17, 2026 at 10:25 pm

    Yes, it means that $i is not accessible from jobs in this way. Unfortunately. So, use another way: for example, param in a script block to be invoked as a job and ArgumentList parameter of Start-Job:

    $i = 42
    $block1 =
    {
        param($i)
        $i * 2
    }
    
    $job1 = Start-Job $block1 -ArgumentList $i
    $job1 | Wait-Job | Receive-Job
    

    Output:

    84
    

    P.S. It is reported that GetNewClosure also does not work with Register-ObjectEvent:
    https://connect.microsoft.com/PowerShell/feedback/details/541754/getnewclosure-doesnt-work-on-register-objectevent

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out this pseudo code. The following is assumed.... I can only
Trying to figure out the best way to accomplish this. I have inhereted a
im trying to figure out how i can do this. i have a list
Trying to figure out how to do this. I have the style but I'd
Trying to figure out why my code isn't working. Basically I have an elseif
Trying to figure out the best way to do this: Should I do something
In trying to figure out this problem (which is still unsolved and I still
Trying to figure out why my vertex shader works on my cell phone (Casio
trying to figure out the best ActiveRecord inheritance strategy for this particular problem: I
Trying to figure out a way to throw out attributes in this data that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.