I have made a script that uses progress bars with the write-progress cmdlet that run fine in ISE or when executed with powershell as a .ps1 but when converted with ps2exe GUI and even with the parameter -noConsole the progress bars are not displayed at all. I havent found any info or solutions to this. I want my exe to be run in console and not with a GUI.
This is a snippet of the code:
$sourceFiles = Get-ChildItem -Path $source -Recurse -File
$destinationFiles = Get-ChildItem -Path $destination -Recurse -File
$matchedFiles = @()
$unmatchedFiles = @()
$totalFiles = $sourceFiles.Count
$filesVerified = 0
# Create a progress bar
$progress = @{
Activity = "Verifying files"
Status = "Elapsed time: 00:00:00"
CurrentOperation = "$filesVerified out of $totalFiles files processed"
PercentComplete = 0
}
# Start the timer
$timer = [System.Diagnostics.Stopwatch]::StartNew()
# Show progress bar before processing the first file
Write-Progress @progress
foreach ($file in $sourceFiles) {
$hash = Get-FileHash $file.FullName -Algorithm MD5
$destinationFile = $destinationFiles | Where-Object { $_.FullName -replace [regex]::Escape($destination), $source -eq $file.FullName }
if ($destinationFile) {
$hashDestination = Get-FileHash $destinationFile.FullName -Algorithm MD5
if ($hash.Hash -eq $hashDestination.Hash) {
Write-Host "$($file.Name) matches" -ForegroundColor Green
$matchedFiles += [PSCustomObject]@{
Name = $file.Name;
SourceHash = $hash.Hash;
DestinationHash = $hashDestination.Hash;
}
} else {
Write-Host "$($file.Name) does not match" -ForegroundColor Red -BackgroundColor Black
$unmatchedFiles += [PSCustomObject]@{
Name = $file.Name;
Directory = $file.DirectoryName
SourceHash = $hash.Hash;
DestinationHash = $hashDestination.Hash;
}
}
} else {
Write-Host "$($file.Name) does not exist in destination" -ForegroundColor Red -BackgroundColor Black
$unmatchedFiles += [PSCustomObject]@{
Name = $file.Name;
SourceHash = $hash.Hash;
DestinationHash = "";
}
}
# Update progress bar
$filesVerified++
$progress.Status = "Elapsed time: $($timer.Elapsed.ToString("hh\:mm\:ss"))"
$progress.CurrentOperation = "$filesVerified out of $totalFiles files processed"
$progress.PercentComplete = ($filesVerified / $totalFiles) * 100
Write-Progress @progress
}
I tried different versions of ps2exe with no luck, also tried running the ps1 with powershell 7, not sure why but I did. The progress bar had a different format but worked.