What I used recently while extracting an exe from a zip file
$newFile = New-Item -Path "C:\Temp" -Name "xyz.exe" # -Force if overriding
try {
# open a writable FileStream
$fileStream = $newFile.OpenWrite()
# create stream writer
$streamWriter = [System.IO.BinaryWriter]::new($fileStream)
# write to stream
$streamWriter.Write($SomeExecutableData)
}
finally {
# clean up
$streamWriter.Dispose()
$fileStream.Dispose()
# if the file is nastily big, [GC]::Collect()
}
There might be a more modern method than [System.IO.BinaryWriter]::new($fileStream)
, I'm too lazy to look it up at the moment but might edit later.
Most of the credit goes to: https://stackoverflow.com/a/70595622/3875151