Skip to content

issue with Write-Host -BackgroundColor in conjunction with Remove-Item #25679

Open

Description

@pier124

Prerequisites

Steps to reproduce

Usually when I use a backgrounded Write-Host I call it this way:
Write-Host "text" -BackgroundColor DarkBlue -NoNewline
and then I add a Write-Host "`n"
in order to make it fit the string and the string ONLY

in certain contextes though when there is a Remove-Item around the trick doesn't work anymore

Expected:
Image

Actual behavior:
Image

Reproducible example:

   # Vars
    $edge_appdata_path = "$($env:LOCALAPPDATA)\Microsoft\Edge\User Data"
    $edge_backup_path = "$($env:USERPROFILE)\Desktop\test\User Data.7z"
    $edge_files_to_backup = @("Default\Preferences","Default\Bookmarks","Default\Bookmarks.bak","Default\Extensions","Default\Secure Preferences","Default\IndexedDB","Local State")

    $temp_backup_dir = "$env:TEMP\EdgeBackupTemp"

    Write-Host "TEST" -ForegroundColor White -BackgroundColor DarkBlue -NoNewline
    Write-Host "`n"
    
    # Create fake
    New-Item -Path $edge_backup_path -Force | Out-Null

    # Create .Bak file
    Copy-Item -LiteralPath $edge_backup_path -Destination "$($edge_backup_path).bak" -Force
    Remove-Item -LiteralPath $edge_backup_path -Force

    # Create structure
    New-Item -ItemType Directory -Path "$temp_backup_dir\User Data" | Out-Null
    New-Item -ItemType Directory -Path "$temp_backup_dir\User Data\Default" | Out-Null

    # Copy
    foreach ($file in $edge_files_to_backup) {
        $temp_source_path = Join-Path -Path $edge_appdata_path -ChildPath $file
        $temp_dest_path = Join-Path -Path $temp_backup_dir -ChildPath "User Data\$file"  
        if (Test-Path $temp_source_path) {
            Copy-Item -Path $temp_source_path -Destination $temp_dest_path -Recurse -Force
        }
    }

    # Desktop archive
    Write-Host "archiving edge files" -ForegroundColor Black -BackgroundColor DarkGray -NoNewline
    Write-Host "`n"
    & 7za a $edge_backup_path "$temp_backup_dir\User Data\*" -t7z -m0=lzma2 -mx7

    # Cleanup temp files
    Remove-Item -Path $temp_backup_dir -Recurse -Force  #TRY TO COMMENT THIS LINE OUT (it should fix it)
    Write-Host "`n`n`n`n"

    Write-Host "TEST" -ForegroundColor White -BackgroundColor DarkBlue -NoNewline
    Write-Host "`n"

For 7za : https://www.7-zip.org/download.html

Expected behavior

Image

Actual behavior

Image

Error details

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.1
PSEdition                      Core
GitCommitId                    7.5.1
OS                             Microsoft Windows 10.0.26100
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Jun 21, 2025
  2. mklement0 commented on Jun 21, 2025

    @mklement0
  3. pier124 commented on Jun 21, 2025

    @pier124
    Author

    Done, you'll need edge and 7za (console version of 7z) to reproduce it

  4. 237dmitry commented on Jun 21, 2025

    @237dmitry

    a minimal reproducible example

    you'll need edge and 7za (console version of 7z) to reproduce it

    I could not reproduce:

    Image

  5. pier124 commented on Jun 22, 2025

    @pier124
    Author

    OS: Windows 11

    Image

    PS: commenting this line out: #Remove-Item -Path $temp_backup_dir -Recurse -Force (line 37) does fix it

  6. pier124 commented on Jun 23, 2025

    @pier124
    Author

    I tested it via 7.6.0-preview.4 and it works (thanks 237dmitry )

  7. added
    WG-NeedsReview
    WG-Interactive-Console
    on Jun 30, 2025
  8. kilasuit commented on Jul 24, 2025

    @kilasuit
    Collaborator

    Investigation

    I dug into this & whilst trying to build a smaller reproducabile example as I didn't think this really required 7za to repro. I noticed pier124 rasied this with an older version of 7za (v24.09) vs the latest release (v25.00) & with the newer release, initially I wasn't able to replicate this in 7.5.1 both with & without 7za. This was both in a Windows Sandbox on Microsoft Windows 10.0.19041 (which is Win10 22h2) & directly on the host for that sandbox.

    I Did start to think that this may then be caused by the 7za program, vs it being a PowerShell Issue, however remembered that we've had a fix to how Progress bar works which also impacts Cmdlets like Remove-Item etc, which off top of my head I do believe this update is only in the preview versions & need to chase down the PR that changed this & will test prior previews shortly.

    pier124 - I'd recommend also updating 7za anyway.

  9. pier124 commented on Jul 25, 2025

    @pier124
    Author

    Ryan Yates (@kilasuit)
    Image

    Still works properly on 7.6.0-preview.4 though

  10. kilasuit commented on Jul 25, 2025

    @kilasuit
    Collaborator

    however remembered that we've had a fix to how Progress bar works which also impacts Cmdlets like Remove-Item etc, which off top of my head I do believe this update is only in the preview versions & need to chase down the PR that changed this & will test prior previews shortly.

    This was added in 7.6.0-preview.1 (hence it working in 7.6.0-preview.4) as part of the following PR #24013 & has not yet been marked for backporting to the 7.5.x releases.

    I'm not gonna spend more time on this, however, I also am wondering if this could crop up again even with #24013 having been merged depending on whether or not this goes beyond the 2 seconds limit that was added in that PR however I've not managed to get a consistent reproducible example that doesn't require either 7za or Edge so it's a really bad example here. I also dont really think that 7za is actually apart of this issue & think it is bleed to the host from how the ProgressBar gets drawn, as I sorta remember this being mentioned in previous issues.

  11. added
    WG-Reviewed
    Waiting on Author
    and removed
    Needs-Triage
    WG-NeedsReview
    on Aug 20, 2025
  12. theJasonHelmick commented on Aug 20, 2025

    @theJasonHelmick
    Collaborator

    Thank you Ryan Yates (@kilasuit) for do this work. pier124, can you re-test to validate this is now working?

  13. pier124 commented on Aug 24, 2025

    @pier124
    Author

    Jason Helmick (@theJasonHelmick) How do I test it? Where do I grab the updated release?

  14. removed
    Waiting on Author
    on Aug 24, 2025
  15. kilasuit commented on Aug 25, 2025

    @kilasuit
    Collaborator

    pier124 - as I mentioned above this hasn't yet been marked for a 7.5.x release and is only in the 7.6.0-previews.

    It may come in 7.5.3 or a later release.

  16. added
    Resolution-No Activity
    on Feb 22, 2026
  17. microsoft-github-policy-service commented on Feb 22, 2026

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  18. added
    Review - Maintainer
    and removed
    WG-Interactive-Console
    WG-Reviewed
    Resolution-No Activity
    on Feb 22, 2026
  19. kilasuit commented on Feb 22, 2026

    @kilasuit
    Collaborator

    The PR that fixed this still needs backporting to the 7.5 release branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Review - Maintainer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions