丸パクリなので、このコードを使用する場合、dobon.netさん を確認してください。
質問者さんとコードが違う部分が無いか確認のために回答として記述します。
(これで動かない場合、恐らく環境が違うと思うので、僕の方では検証できません。)
PowerShell
1 add-type - AssemblyName microsoft . VisualBasic
2 add-type - AssemblyName System . Windows . Forms
3
4
5 $cscode = @ "
6 public static void ActiveWindow(IntPtr hWnd)
7 {
8 if (hWnd == IntPtr.Zero)
9 {
10 return;
11 }
12
13 //ウィンドウが最小化されている場合は元に戻す
14 if (IsIconic(hWnd))
15 {
16 ShowWindowAsync(hWnd, SW_RESTORE);
17 }
18
19 //AttachThreadInputの準備
20 //フォアグラウンドウィンドウのハンドルを取得
21 IntPtr forehWnd=GetForegroundWindow();
22 if (forehWnd == hWnd)
23 {
24 return;
25 }
26 //フォアグラウンドのスレッドIDを取得
27 uint foreThread = GetWindowThreadProcessId(forehWnd, IntPtr.Zero);
28 //自分のスレッドIDを収得
29 uint thisThread = GetCurrentThreadId();
30
31 uint timeout = 200000;
32 if (foreThread != thisThread)
33 {
34 //ForegroundLockTimeoutの現在の設定を取得
35 //Visual Studio 2010, 2012起動後は、レジストリと違う値を返す
36 SystemParametersInfoGet(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, ref timeout, 0);
37 //レジストリから取得する場合
38 //timeout = (uint)Microsoft.Win32.Registry.GetValue(
39 // @" HKEY_CURRENT_USER\Control Panel\Desktop ",
40 // " ForegroundLockTimeout ", 200000);
41
42 //ForegroundLockTimeoutの値を0にする
43 //(SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)を使いたいが、
44 // timeoutがレジストリと違う値だと戻せなくなるので使わない
45 SystemParametersInfoSet(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, 0);
46
47 //入力処理機構にアタッチする
48 AttachThreadInput(thisThread, foreThread, true);
49 }
50
51 //ウィンドウをフォアグラウンドにする処理
52 SetForegroundWindow(hWnd);
53 SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0,
54 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_ASYNCWINDOWPOS);
55 BringWindowToTop(hWnd);
56 ShowWindowAsync(hWnd, SW_SHOW);
57 SetFocus(hWnd);
58
59 if (foreThread != thisThread)
60 {
61 //ForegroundLockTimeoutの値を元に戻す
62 //ここでも(SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)は使わない
63 SystemParametersInfoSet(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, timeout, 0);
64
65 //デタッチ
66 AttachThreadInput(thisThread, foreThread, false);
67 }
68 }
69
70 [DllImport(" user32 . dll ")]
71 [return: MarshalAs(UnmanagedType.Bool)]
72 private static extern bool SetForegroundWindow(IntPtr hWnd);
73
74 [DllImport(" user32 . dll ")]
75 private static extern IntPtr GetForegroundWindow();
76
77 [DllImport(" user32 . dll ", SetLastError = true)]
78 [return: MarshalAs(UnmanagedType.Bool)]
79 private static extern bool BringWindowToTop(IntPtr hWnd);
80
81 [DllImport(" user32 . dll ")]
82 static extern IntPtr SetFocus(IntPtr hWnd);
83
84 [DllImport(" user32 . dll ", SetLastError = true)]
85 [return: MarshalAs(UnmanagedType.Bool)]
86 private static extern bool SetWindowPos(IntPtr hWnd,
87 int hWndInsertAfter, int x, int y, int cx, int cy, int uFlags);
88
89 private const int SWP_NOSIZE = 0x0001;
90 private const int SWP_NOMOVE = 0x0002;
91 private const int SWP_NOZORDER = 0x0004;
92 private const int SWP_SHOWWINDOW = 0x0040;
93 private const int SWP_ASYNCWINDOWPOS = 0x4000;
94 private const int HWND_TOP = 0;
95 private const int HWND_BOTTOM = 1;
96 private const int HWND_TOPMOST = -1;
97 private const int HWND_NOTOPMOST = -2;
98
99 [DllImport(" user32 . dll ")]
100 [return: MarshalAs(UnmanagedType.Bool)]
101 private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
102 [DllImport(" user32 . dll ")]
103 [return: MarshalAs(UnmanagedType.Bool)]
104 private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
105
106 private const int SW_SHOWNORMAL = 1;
107 private const int SW_SHOW = 5;
108 private const int SW_RESTORE = 9;
109
110 [DllImport(" user32 . dll ")]
111 [return: MarshalAs(UnmanagedType.Bool)]
112 private static extern bool IsIconic(IntPtr hWnd);
113
114 [DllImport(" user32 . dll ")]
115 private static extern uint GetWindowThreadProcessId(
116 IntPtr hWnd, IntPtr ProcessId);
117
118 [DllImport(" kernel32 . dll ")]
119 private static extern uint GetCurrentThreadId();
120
121 [DllImport(" user32 . dll ")]
122 [return: MarshalAs(UnmanagedType.Bool)]
123 private static extern bool AttachThreadInput(
124 uint idAttach, uint idAttachTo, bool fAttach);
125
126 [DllImport(" user32 . dll ", EntryPoint = " SystemParametersInfo ",
127 SetLastError = true)]
128 [return: MarshalAs(UnmanagedType.Bool)]
129 private static extern bool SystemParametersInfoGet(
130 uint action, uint param, ref uint vparam, uint init);
131
132 [DllImport(" user32 . dll ", EntryPoint = " SystemParametersInfo ",
133 SetLastError = true)]
134 [return: MarshalAs(UnmanagedType.Bool)]
135 private static extern bool SystemParametersInfoSet(
136 uint action, uint param, uint vparam, uint init);
137
138 private const uint SPI_GETFOREGROUNDLOCKTIMEOUT = 0x2000;
139 private const uint SPI_SETFOREGROUNDLOCKTIMEOUT = 0x2001;
140 private const uint SPIF_UPDATEINIFILE = 0x01;
141 private const uint SPIF_SENDCHANGE = 0x02;
142 " @
143
144 $Win32 = add-type - memberDefinition $cscode - name "Win32ApiFunctions" - passthru
145
146 $ps = Get-Process | Where-Object { $_ . Name -match "notepad" }
147 foreach ( $process in $ps ) {
148 $Win32 ::ActiveWindow ( $process . MainWindowHandle ) ;
149 }
150
151 Start-Sleep - s 30
152
153 $ps = Get-Process | Where-Object { $_ . Name -match "cmd" }
154 foreach ( $process in $ps ) {
155 $Win32 ::ActiveWindow ( $process . MainWindowHandle ) ;
156 }