PowerShell / PowerShell/PowerShell

Experimental `PSRedirectToVariable` feature: target variable isn't cleared if the captured stream has no output

Open
#24,730 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs-Triage WG-Engine WG-NeedsReview
Dominant language
C#
Stars
55.5k
Forks
8.5k
Avg merge
1d 2h
Merged PRs (30d)
88

Description

Prerequisites
Steps to reproduce

The experimental PSRedirectToVariable feature conveniently allows capturing stream output via > and >> in variables, e.g. >variable:stdout.

If the targeted stream has no output, the variable should be cleared to indicate that fact (if appending is desired, >> can be used).
However, any preexisting variable value currently unexpectedly lingers.

# Note: Enable-ExperimentalFeature PSRedirectToVariable must be in effect
# (turning it on requires starting a new session.)
$stdout = 'psych!'
whoami -nosuch >variable:stdout 2>$ull
$stdout
Expected behavior

No output, because no stdout output was received, which should set $stdout to the enumerable null ([System.Management.Automation.Internal.AutomationNull]::Value), whether or not the variable previously existed.

This would be consistent with the behavior of direct assignments; e.g., $stdout = & {} properly records the enumerable null.

Actual behavior
psych!

That is, the preexisting value of the target variable unexpectedly lingered.

Error details

No response

Environment data
PowerShell 7.5.0-rc.1
Visuals

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Enable the PSRedirectToVariable experimental feature and run the reproduction using $stdout, whoami -nosuch, and >variable:stdout. Trace the PSRedirectToVariable handling for a stream with no output, then verify that an existing target variable becomes the enumerable null and that the append behavior remains distinct.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.