How can I get the foreground Window using batch?

748 views Asked by At

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 :)

1

There are 1 answers

0
Peter Badida On BEST ANSWER

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