2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PowerShell】Powershellの起動時にログを生成するショートカット作成

Last updated at Posted at 2020-01-12

はじめに

Powershellで作業するときにログを残して置きたい時ってありませんか。
また、TeraTermのように起動時に自動でログを作成してほしい時ってありませんか。
そんな、あなたのためにPowershell起動時に自動でログを作成するショートカットを作成します。

事前準備

ログの保存先として、C:\log\を作成します。
保存先は任意に変更しても構いません。

ショートカット作成

右クリックし、ショートカットの作成を押下します。そして、ショートカットの設定は以下の通りです。

  • リンク先
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe "-NoExit" "-Command" $date = Get-Date -Format "yyyyMMdd_ssmm"; Start-Transcript -Path C:\log\$date.log

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exeは環境によって変更する可能性があります。
C:\log\$date.logは任意の保存先に変更してください。
$dateはdateコマンドの結果を変数に格納しています。

  • 作業フォルダ
%HOMEDRIVE%%HOMEPATH%

作業フォルダを%HOMEDRIVE%%HOMEPATH%にしないとホームディレクトリが%SystemRoot%\system32\WindowsPowerShell\v1.0\のままになります。
ショートカットを実行すると、Powershellが起動し、C:\log\<yyyyMMdd_ssmm>.logのログファイルが作成されます。イメージで言うと、TeraTermのログみたいな感じですね。

ショートカットの説明

ショートカットについて説明します。
ショートカットをカスタマイズしたい方はお読みください。

powershell.exe "-NoExit" "-Command"

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe "-NoExit" "-Command"

-NoExitはPowershellを起動しっぱなしにする処理です。
-Command以下の処理を実行した後、通常は閉じられますが、-NoExitによって閉じないようにしてます。

$date = Get-Date -Format "yyyyMMdd_ssmm"

$date = Get-Date -Format "yyyyMMdd_ssmm";

ファイル名を日付で一意にするために作成しています。
;を使用することでコマンドが続けて実行されます。

commandA ; commandB

を使用することで、commandA ; commandBが続けて実行されます。

Start-Transcript

Start-Transcript -Path C:\log\$date.log

Powershellをログとして出力するコマンドです。
-Pathでログの出力先を指定します。

その他のオプションについては以下をご覧ください。
[https://docs.microsoft.com/ja-jp/previous-versions/powershell/module/Microsoft.PowerShell.Host/Start-Transcript?view=powershell-3.0]

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?