1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

コマンドプロントを最前面に

Last updated at Posted at 2025-06-18

Windowsで「active」と打つとコマンドプロンプトを常に最前面にする方法

コマンドプロンプト作業中、常にウィンドウを前面に表示しておきたいときに便利なスクリプトです。
この方法では、どこにいても active と入力するだけで、そのコマンドプロンプトウィンドウが「常時最前面」になります。


スクリプト内容(active.cmd)

以下を active.cmd という名前で保存してください。

@echo off
powershell -NoProfile -Command "& {
  Add-Type -TypeDefinition @'
    using System;
    using System.Runtime.InteropServices;
    public class WinAPI {
      [DllImport(\"user32.dll\")]
      public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
      public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
      public const UInt32 SWP_NOMOVE = 0x0002;
      public const UInt32 SWP_NOSIZE = 0x0001;
      public const UInt32 SWP_SHOWWINDOW = 0x0040;
    }
'@ -Language CSharp

  $hwnd = (Get-Process -Id $PID).MainWindowHandle
  [WinAPI]::SetWindowPos($hwnd, [WinAPI]::HWND_TOPMOST, 0, 0, 0, 0, [WinAPI]::SWP_NOMOVE -bor [WinAPI]::SWP_NOSIZE -bor [WinAPI]::SWP_SHOWWINDOW)
}"

配置手順

  1. 任意のディレクトリに保存:
mkdir %USERPROFILE%\bin
move active.cmd %USERPROFILE%\bin
  1. %USERPROFILE%\bin を Path に追加:
  • 環境変数の設定 → ユーザー環境変数 → Path を編集
  • 新規 を押して %USERPROFILE%\bin を追加

使い方

コマンドプロンプトでどこからでも以下を実行:

active

そのコマンドプロンプトが「常に前面に表示」されるようになります。


備考

  • 効果はウィンドウを閉じるまで持続します。
  • 無効化したい場合は、AutoHotkey などで解除操作を行うか、スクリプトを改造する必要があります。
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?