Hi @Vincent Hurenkamp ,
Thank you for your patience, and I apologize for the delayed response. I appreciate the details you've shared about your launcher design.
I've reviewed your question, the existing response, and the relevant Microsoft documentation. Based on my review, keeping the script open with FILE_SHARE_READ does not, by itself, guarantee that pwsh –File will execute the exact same file object. I'd like to share a few points that may help clarify this:
When you open the script without FILE_SHARE_WRITE or FILE_SHARE_DELETE, Windows restricts subsequent conflicting write or delete opens while the handle remains open. You can also use GetFileInformationByHandleEx(FileIdInfo) to identify the opened file using its volume serial number and file ID. However, this does not establish the identity of a file later opened by PowerShell. See CreateFileW and FILE_ID_INFO.
You should also protect the script's parent directories against unauthorized changes and account for any symbolic links or junctions along the path. While GetFinalPathNameByHandleW can retrieve the resolved path, it does not guarantee that a later lookup will reach the same file object. Directory rename operations are also subject to restrictions involving existing open handles. See File Security and Access Rights, Reparse Points and File Operations, and GetFinalPathNameByHandleW.
Since pwsh –File accepts a script path rather than an existing file handle, the launcher's validation and PowerShell's script loading should be treated as separate operations. The PowerShell source code also shows how external scripts can be read through a pathname-based FileStream. However, this implementation detail does not establish an end-to-end guarantee that PowerShell will execute the exact file object previously validated by the launcher. If strict identity is required, you may need to consider an approach that avoids reopening the script through a potentially mutable path. See about_Pwsh.
I hope this helps clarify the documented guarantees and their limitations. If you find my response useful, I’d truly appreciate your feedback on my answer by following this guidance.
Thank you.