# Execute wmplayer.exe, but don't transfer active session to it

**URL:** <https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170>\
**Category:** PowerShell Help\
**Created:** [April 8, 2016, 6:19pm UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170 "2016-04-08T18:19:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyewhang2-chung](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cyewhang2-chung](https://forums.powershell.org/u/cyewhang2-chung)\
**Post date:** [April 8, 2016, 6:19pm UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170/1 "2016-04-08T18:19:08Z")

</div>

wrote a script which execute wmplayer.exe to play a sound file, then ask a question or hit enter key to listen to the next sound. But currently I need to move the mouse’s pointer and click on the Powershell’s console/shell to enter the answer or hit enter key to continue. Is there a way which I can execute an external program/ applet/ application, but the user active windows dialog session is still be with the Powershell’s console which activated/called the external program (example wmplayer.exe)? Or way to transfer back the activate user session.

Thanks!

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 10, 2016, 9:51pm UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170/2 "2016-04-10T21:51:14Z")

</div>

I think this will help you a bit. You can use this code to focus to any window you want.

```
Add-Type -TypeDefinition @"
  using System;
  using System.Runtime.InteropServices;
  public class SFW {
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     public static extern bool SetForegroundWindow(IntPtr hWnd);
  }
"@

$app = (Get-Process -Name wmplayer).MainWindowHandle
[SFW]::SetForegroundWindow($app)
```

---

<div class="post-metadata">

**Author:** ![cyewhang2-chung](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cyewhang2-chung](https://forums.powershell.org/u/cyewhang2-chung)\
**Post date:** [April 11, 2016, 1:06am UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170/3 "2016-04-11T01:06:30Z")

</div>

## Thanks Richard ! I tried this;

Add-Type…etc… as what you had provided..  
$app = (Get-Process -Name powershell).MainWindowHandle

## ./wmplayer file.mp3 [SFW]::SetForegroundWindow($app)

And it worked.. It did switch back to powershell and make it the active session…  
But when I add into my script, it did not work.. still troubleshooting.. B(

Will update again.. hope I can resolve it. Oh.. I only had one powershell process running.

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 11, 2016, 1:35am UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170/4 "2016-04-11T01:35:21Z")

</div>

Can you show the script that you made?

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [April 11, 2016, 10:55pm UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170/5 "2016-04-11T22:55:24Z")

</div>

If you have it added to your script just after you launch wmplayer, it could very easily be a timing issue. The code is looking for the powershell process and bringing it to the foreground before the system displays the UI for wmplayer.

Since you have not provided the code you have been playing with, it’s hard to say what your options are. There are many ways to execute wmplayer in code. For example. If you are using Start-Process, you could use the -WindowStyle parameter to start the process minimized. Then it will not be on screen to take focus away from the powershell window. Providing a sample of what you are trying will definitely help in getting answers relevant to the challenge.

---

<div class="post-metadata">

**Author:** ![cyewhang2-chung](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cyewhang2-chung](https://forums.powershell.org/u/cyewhang2-chung)\
**Post date:** [April 14, 2016, 1:40am UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170/6 "2016-04-14T01:40:01Z")

</div>

THANKS Richard and Curtis!

I have no issue to send you guys the script, it is actually my first Powershell script… a mini task to learn PS. About 200 lines… it read and write to a CVS file. Below are section of the script. If you need to the whole script and CVS, I will upload it somewhere.

BTW, I tried adding ‘start-sleep -5’ between wmplayer and [SFW]::SetForegroundWindow($app), but it don’t help.

```
Add-Type -TypeDefinition @"
  using System;
  using System.Runtime.InteropServices;
  public class SFW {
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     public static extern bool SetForegroundWindow(IntPtr hWnd);
  }
"@
$app = (Get-Process -Name powershell).MainWindowHandle

#ToTestNext =2 mean new word added this round
$PSspellRoot = "C:\Users\PSspell"
$output = "$PSspellRoot\"
[int16]$Max = 10 #If Max not reached, add more new words
[int16]$MaxR = 10 #Max Reenforced Word

$Words2 = Import-csv ".\WordBanks2.csv" | Select @{Name="Serial"; Expression={[int16]$_.Serial}}, `
             Word,Meaning,`
             @{Name="SoundFile"; Expression={[int16]$_.SoundFile}}, `
             @{Name="ToTestNext"; Expression={[int16]$_.ToTestNext}}, ` 
             @{Name="NumberWrong"; Expression={[int16]$_.NumberWrong}}, `
             @{Name="NumberCorrect"; Expression={[int16]$_.NumberCorrect}}, `
             @{Name="Difficulty"; Expression={[int16]$_.Difficulty}}, `
             @{Name="ConsecutiveCounter"; Expression={[int16]$_.ConsecutiveCounter}}, `
             @{Name="RepetitionCounter"; Expression={[int16]$_.RepetitionCounter}}

$Lookup = Import-csv ".\LookUpRepeat.csv" | Select @{Name="1"; Expression={[int16]$_.1}}, `
            @{Name="2"; Expression={[int16]$_.2}}, @{Name="3"; Expression={[int16]$_.3}}, ` 
            @{Name="4"; Expression={[int16]$_.4}}, @{Name="5"; Expression={[int16]$_.5}}

#==== other lines ========== not shown here.. basically to select words to test.

$c=0 # counter to locate which word row
foreach ($i in $Words2.ToTestNext)
    { if ($i -eq 1) 
        { $w = $Words2[$c].word
        ./wmplayer $output$w".mp3"
        Start-Sleep -s 5 #test
        [SFW]::SetForegroundWindow($app)

===other lines ====
```

Basically, it is for spelling. The CVS file record words and other info, the same word spelling are re-enforced depending on the word difficulty and consecutive the word are being spelled correctly. The script can be shorten by using function…

Added: 19 April : after I add the Start-sleep I notice the flashing cursor appeared back on the Powershell session, but I still need to click on the session windows to enable keyboard entry.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/execute-wmplayer-exe-but-dont-transfer-active-session-to-it/6170/7 "2024-05-16T20:41:34Z")

</div>

Closed on May 16, 2024, 8:41pm UTC


