Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PowerShell class methods execute external commands twice #26067

@NicoJanE

Description

@NicoJanE

Prerequisites

Steps to reproduce

  1. Make sure to have a WSL named "NewDistro" with a user "nico" that can run sudo without password or adjust the distro/user in the code below.
  2. Run the sample program: powershell.exe -ExecutionPolicy Bypass -File ".\Powershell-class-bug.ps1"
  3. Observe that sudo prompts appear twice for class methods, once for functions
  4. This affects any external process execution, not just WSL/sudo
  5. Commands that modify state will execute their changes twice

Impact: This duplicate execution causes multiple serious issues:

  • Interactive commands prompt for input twice (passwords, confirmations)
  • Commands with side effects execute twice (file creation, database operations)
  • Performance degradation due to unnecessary duplicate operations
  • Inconsistent behavior between class methods and regular functions
  • Makes class-based architectures unreliable for external process automation

Expected behavior

Class methods and functions should execute external commands identically

Actual behavior

Class instance methods execute external commands twice

Error details

Run sample program

Environment data

- PowerShell Version:7.5.3
- PowerShell Version: 5.1.26100.6584
- OS: Microsoft Windows NT 10.0.26100.0
- WSL Version:
  - WSL-versie: 2.4.13.0
  - Kernelversie: 5.15.167.4-1
  - WSLg-versie: 1.0.65
  - MSRDC-versie: 1.2.5716
  - Direct3D-versie: 1.611.1-81528511
  - DXCore-versie: 10.0.26100.1-240331-1435.ge-release
  - Windows-versie: 10.0.26100.6584

Visuals

powershell5-class-bug.txt
powershell7-class-bug.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.Waiting on AuthorThe PR was reviewed and requires changes or comments from the author before being accept

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions