79340704

Date: 2025-01-08 20:44:42
Score: 1
Natty:
Report link

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

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): What I use
  • Low reputation (0.5):
Posted by: 9 Guy