MISO
MISO

Reputation: 75

How can I get the foreground Window using batch?

I tried to get the foreground Window and write it in a file. After a few tries, I've only got the task list. Now I discovered this Code, but it won't work for me:

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class Tricks {
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
}
"@

$a = [tricks]::GetForegroundWindow()

get-process | ? { $_.mainwindowhandle -eq $a } 

Thanks :)

Upvotes: 2

Views: 890

Answers (1)

Peter Badida
Peter Badida

Reputation: 12159

The code you have is a Powershell snippet. Save that file as something.ps1 or run it from cmd.exe with this:

powershell something.ps1

Upvotes: 3

Related Questions