Complete task statement: Schedule scan current processes in the system, if there is no notepad process, then start it.

I have never worked with PowerShell, but the task has been set and it needs to be solved. I tried to do this:

IF (Get-Process notepad.exe -ErrorAction SilentlyContinue) { } ELSE { Start-Process notepad.exe } 

But the notebook always starts. Any ideas?

    2 answers 2

    Try

     Get-Process notepad 

    On my machine (notepad.exe is running):

     PS D:\> Get-Process notepad Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 67 7 2620 6956 88 0.03 2360 notepad PS D:\> Get-Process notepad.exe Get-Process : Cannot find a process with the name "notepad.exe". Verify the process name and call the cmdlet again. At line:1 char:12 + Get-Process <<<< notepad.exe + CategoryInfo : ObjectNotFound: (notepad.exe:String) [Get-Process], ProcessCommandException + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand 
    • So it does not work either. My notebook is not running, but the team still finds something. PS C: \ Windows \ system32> Get-Process notepad Handles NPM (K) PM (K) WS (K) VM (M) CPU (s) Id ProcessName ------- ------ - - --- ----- ----- ------ - ----------- 72 8 1468 532 36 0.09 5520 notepad - maestro
    • @Alouette: Hm, are you sure? And what is the process with Id = 5520? - VladD
    • This is something prohibitively unintelligible. In the task manager, a process with that name and such a PID does not appear. In cmd, I entered the taskkill / pid 5520 / f command, he pretended to kill this process. After that, the program has earned. How to explain it? - maestro
    • Zombie state process? Try using Process Explorer instead of Task Manager in such cases. - VladD
    • Maybe the process was launched in another explorer? And you just did not see it on the screen or it was launched without displaying the window. - pincher1519
     if ( -not (get-process | where {$_.ProcessName -eq 'notepad'})) { 'not run. Start Running' Start-Process -FilePath 'c:\windows\notepad.exe' } else {'Arlready exist'}