Before we dive too deep into the details - what is it actually what you’re trying to do? Try do describe the end goal not the way you think you need to go to achieve it.
If you want to read a text file in PowerShell you usually do it with
Get-Content -Path 'c:\ps-test\test.txt'
If you want to check if a particular process is running you’d check it with
In most of the cases “Opening” a text file does not mean to “lock” it for other processes like you may expect it or you may know it from office documents or executables. That’s why I’m asking.
That depends pretty much on the program you’re using to open the text file. If it’s Notepad++ for example the text file you’re after has to have the focus inside Notepad++.
That depends pretty much on your requirement. Opening a text file is different to opening an office document for example. When you open an office document it will be kept open and blocked for other applications or processes until you close it. Opening a plain text file unsually does not block it for other processes. So if you just want to know if your notepad application has read the content of the text file then you’re good with the code you already posted.