Open only if not open (specific text file and executable)

With the following code I can open a text file (c:\ps-test\test.txt):

Start-Process -FilePath $env:HOMEDRIVE\ps-test\test.txt

But how to open a specific text file (test.txt) when not open? How can you check this?

And how to do the same for an executable? For example “charmap.exe”.

Thanks in advance for your feedback, help and time.

dreivilo47,
Welcome to the forum. :wave:t4:

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

Get-Process -Name 'charmap'
1 Like

Thank you @Olaf.

Thanks for your help, I’ve found a solution for an executable:

$ProcessRunning = Get-Process -Name 'charmap' -ErrorAction SilentlyContinue
If (-Not $ProcessRunning) {Start-Process -FilePath charmap.exe}

The end goal is to open the text file ‘c:\ps-test\test.txt’ when it isn’t already open. Not to read a text file.

Great. :+1:t4:

I’m curious …

What exactly do you mean with “open a text file”?

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. :wink:

I was thinking along the sames lines as Olaf. I took a quick look and this might work for you:

Get-Process | Where-Object {$_.MainWindowTitle -Match 'txt'}
1 Like

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++. :wink:

Totally Agree. Thanks Olaf. Not an easy task to make bullet proof.

@Olaf, what exactly do I mean with “open a text file”? Open a text file is for me: open the text file on screen so I can see the content.

I’ve found a solution for a text file too, thank you @tonyd for your post:

$TxtFileOpen = Get-Process | Where-Object {$_.MainWindowTitle -Match 'test.txt'}
If (-Not $TxtFileOpen) {Start-Process -FilePath $env:HOMEDRIVE\ps-test\test.txt}

I use Notepad and not Notepad++.

@Olaf and @tonyd, thank you for your help and support. I appreciate this. My problem is solved.

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. :+1:t4: :wink: :slightly_smiling_face:

1 Like

Thank you for the information @Olaf.

This is the solution for the text file and the executable:

$ProcessRunning = Get-Process -Name 'charmap' -ErrorAction SilentlyContinue
If (-Not $ProcessRunning) {Start-Process -FilePath charmap.exe}

$TxtFileOpen = Get-Process | Where-Object {$_.MainWindowTitle -Match 'test.txt'}
If (-Not $TxtFileOpen) {Start-Process -FilePath $env:HOMEDRIVE\ps-test\test.txt}