Search code examples
powershellprocesspid

Closing a command prompt in powershell


I have a powershell script, which opens up many cmd.exe processes, having different titles. I wish to close only one of these processes. I cannot use

Get-Process cmd | Stop-Process

as all the processes shut down. Is there a way to set the process ID for each process, so I can identify it later and shut it down? Or is there any other way to identify the process by its title name?


Solution

  • Store the information about a process in a variable. Later you can stop the process with those informations

    $XYProcess = Start-Process "cmd.exe" -PassThru
    
    Stop-Process $XYProcess.ID