binarywrite.ps1

<#PSScriptInfo
 
.VERSION 1.61
 
.GUID e6d6be4d-1212-448f-8276-66799b69a2c8
 
.AUTHOR chrdg kd.
 
.COMPANYNAME me et al.
 
.COPYRIGHT 2023
 
.TAGS large file, deletion, delete, file replace, byte writer
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES Run as per cmdlet instructions for one file, runtime depends on total file size.
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Large file overwrite (files >1GB)
 
#>
 

Param()


$fileName = Read-Host "Write the full file name [must be under the same directory]:`r`n Whole process takes ~14mins/file";

# Clear all previous timers.
Get-EventSubscriber | Unregister-Event;

# Generated data for 1MB header parts.
$frameSize = 1MB;
$prngPart = [System.Security.Cryptography.RNGCryptoServiceProvider]::new();
$prngBytes = New-Object byte[] $frameSize; $prngPart.GetBytes($prngBytes);

# Main file size for bytes replacement 1Gb-10Gb recommended size ranges.
$allBytes = [System.IO.File]::ReadAllBytes("C:\Users\$($fileName)");

# Default Sizes, timers etc.
$midSect = [Math]::Round($allBytes.length/2);
$midRange = $midSect+$prngBytes.length;
$EndRange = $allBytes.length-$prngBytes.length;

# Main running process will terminate after X seconds (X=Round(($midSect)/36)/10)+1KB
$timelapse = [Math]::Round($midSect / 36/10)+1KB;

$timer = New-Object timers.timer
$timer.Interval = $timelapse # set to ~14min interval standard time.
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Output -Action {Write-Host "Auto Ctrl+C"; Stop-Process $pid} | Out-Null;
$timer.Enabled = $true

# Start loops etc.
$r=$j=$k=0;
$runtotal=Measure-Command {
0..$prngBytes.length | %{ $allBytes[$r] = $prngBytes[$r]; $r++ }

$midSect..$midRange | %{ $allBytes[$j] = $prngBytes[$j]; $j++ }

$allBytes.length..$EndRange | %{ $allBytes[$k] = $prngBytes[$k]; $k++ }

while ($true) {

   sleep 1
[System.IO.File]::WriteAllBytes("C:\Users\$($fileName)",$allBytes)

 }
}

$runtotal | Set-Content "C:\Users\timedexec.out";

# Opt: Manual Ctrl+C try to stop/break the previous command/infinite loop
# [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
# [System.Windows.Forms.SendKeys]::SendWait("^{c}")