Skip to content

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

@pier124

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    WG-Interactive-Consolethe console experienceWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions