Please feel free to provide feedback or file bugs here.

PowerShell copy on write behavior depends on operator type

Votes from Connect: 3

Original Date Submitted: 5/18/2015 2:02:46 AM

Description:
********Contact Information********
Handle: Sergey Teplyakov
Site Name: PowerShell
Feedback ID: 1341920
***************************************

Frequency: PowerShell
Regression:

Problem Description:
PowerShell uses copy-on-write semantic for inner scopes.

This means that if you'll change outer variable in the inner scope then inner scope would deal with it's own copy. Here is an example:

$list = @(1, 2)

Function Foo
{
"Foo: initial value - $list"
$list = $list + 3
"Foo: `$list: $list, `$global:list: $global:list"
}
The output would be:

Foo: initial value - 1 2
Foo: $list: 1 2 3, $global:list: 1 2
Ok, this behavior is by design. But lets change Foo function a bit:

Function Foo
{
"Foo: initial value - $list"
**$list += 3**
"Foo: `$list: $list, `$global:list: $global:list"
}
The output would be:

Foo: initial value - 1 2
Foo: $list: **3**, $global:list: 1 2

Hm... I can assume that x += y operator is not exactly the same that x = x + y. But that is not exacted. Let's change the code once more:

Function Foo
{
**New-Variable -Name z -Value 42**
"Foo: initial value - $list"
$list += 3
"Foo: `$list: $list, `$global:list: $global:list"
}

And now the output would be:

Foo: initial value - 1 2
Foo: $list: 1 2 3, $global:list: 1 2

It seems like a bug. The only question is where exactly? I assume behavior of += in second case is incorrect. But maybe I missing something...

Link to stackoverflow post: http://stackoverflow.com/questions/30290555/powershell-copy-on-write-behavior-depends-on-operator-type

Product Studio item created by Connect Synchronizer due to creation of feedback ID 1341920 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=1341920).

Repro Steps:
Expected Results:

Internal BugId: 14479

1 vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)

    We’ll send you updates on this idea

    AdminJoey Aiello [MSFT] (Program Manager, Windows Server) shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    0 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base