LoginSignup
23
28

More than 5 years have passed since last update.

PowerShellで日時をプロンプトに表示する

Posted at

以前、PowerShellのプロンプトを短縮するという記事を書きましたが、今回は、さらにプロンプトに現在日時を表示します。

プロンプトに日時を表示することで、「あれ?このコマンドっていつ打ったんだっけ?」とか「しまった、作業の開始時間記録するの忘れた」などを履歴から取得することが出来るようになります。

現在日時の取得

現在日時はGet-Dateコマンドで取得できます。
さらに、-Formatで、任意の形式の文字列に変換可能です。

> Get-Date
2014年5月13日 13:24:57

> Get-Date -Format "yyyy/MM/dd HH:mm"
2014/05/13 13:24

プロンプトに表示

1.MyDocumentsにWindowsPowerShellフォルダを作成
2.作成したフォルダ内に以下のファイルを作成

Microsft.PowerShell_profile.ps1
function global:prompt {
  $now = Get-Date -format "yyyy/MM/dd HH:mm"
  Write-Host($now + " ") -nonewline
  return "> "
}

3.以下の様に時刻が表示されます。
2014-05-13_13h31_29.png

参考

23
28
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
23
28