よせなべ

プログラミングを入れた鍋にガジェットやゲームなど様々な材料を入れて煮込みます。

コマンドライン(CUI)で Windows Defender

f:id:cpthgli:20170215202841p:plain

概要

 アンチウイルスソフト Windows Defenderをコマンドラインで使用して、ファイルのウィルスチェックを行うための方法を紹介します。

経緯

 先日、「 Mozillaの元開発者である Robert O'Callahan氏が、 Microsoft以外のアンチウイルス製品は使用すべきでないを主張している。」という記事を見かけました。

security.srad.jp

その後、 Googleのエンジニアである Justin Schuh氏も、「行儀のいいアンチウイルスと呼べるのは Windows Defenderだけである。 Chromeのセキュリティメカニズムを壊したことのない唯一の存在。」と述べています。

security.srad.jp

そこで、 外部ソフトから Windows Defenderを使用するためにコマンドラインでウイルスチェックをする方法を調べてみました。

Windows Defenderをコマンドラインで使用する。

使用方法

 まず、コマンドラインから使用するための実行ファイルですが、"MpCmdRun.exe"を使用します。
パスは、"C:\Program Files\Windows Defender\MpCmdRun.exe"です。
 ヘルプオプションをつけて実行してみます。

C:\Program Files\Windows Defender>MpCmdRun.exe -?
Microsoft Antimalware Service Command Line Utility (c) 2006-2015 Microsoft Corp
Use this tool to automate and troubleshoot Microsoft Antimalware Service

Usage:
MpCmdRun.exe [command] [-options]

Command Description
   -? / -h                                    Displays all available options
                                              for this tool
   -Scan [-ScanType #] [-File <path> [-DisableRemediation] [-BootSectorScan]]
         [-Timeout <days>]
                                              Scans for malicious software

~~~略~~~

Additional Information:

Support information will be in the following directory:
C:\ProgramData\Microsoft\Windows Defender\Support

   -Scan [-ScanType value]
        0  Default, according to your configuration
        1  Quick scan
        2  Full system scan
        3  File and directory custom scan

           [-File <path>]
                Indicates the file or directory  to be scanned, only valid for custom scan.

~~~略~~~

-Scanコマンドでスキャン。ファイルやフォルダを対象にしたい場合は、 -ScanType 3 -File <path>オプションを使用すれば良さそうです。

使用例

 "D:\<user-name>\Downloads\"ディレクトリに、"test.txt"を作成して、テストを行います。

D:\<user-name>\Downloads>"C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 3 -File "D:\<user-name>\Downloads\test.txt"
Scan starting...
Scan finished.
Scanning D:\<user-name>\Downloads\test.txt found no threats.

Scanning D:\<user-name>\Downloads\test.txt found no threats.*1と出ました。安心して使用できますね()。

まとめ

 コマンドラインから Windows Defenderを使用して対象のファイルやフォルダのウイルスチェックを行うコマンドは、

"C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 3 -File <path>

です。
環境変数に、"C:\Program Files\Windows Defender\"を追加して、

MpCmdRun -Scan -ScanType 3 -File <path>

で使用できるようにすると便利かもしれません。

みなさんも、ウイルス対策ソフトを見直して Windows Defenderを使って見ませんか?

*1:D:\<user-name>\Downloads\test.txtに脅威はありません。