LoginSignup
2
3

More than 3 years have passed since last update.

PowerShellのプロンプトにカレントディレクトリ名だけ表示

Last updated at Posted at 2019-12-25

PowerShell 6.2.3

デフォルトだとフルパスが表示されて階層が深くなるほど横に伸び邪魔になる。
とりあえずカレントディレクトリ名だけ表示したい。

PS C:\Users\******\Documents\Downloads>

PowerShellのプロンプトはprompt関数でカスタマイズ出来る。
PowerShell profileに書いておけば起動時に勝手に読み込まれる。

PowerShell profileのパスは$PROFILE環境変数で参照出来る。

PS C:\Users\******\Documents\Downloads> $PROFILE
C:\Users\******\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

code $PROFILEとかでファイルを開いて以下を書く

Microsoft.PowerShell_profile.ps1
function prompt {
    'PS ' + $(split-path . -Leaf) + '> '
}

split-pathは指定パスから特定の要素だけ取り出すコマンドレット、-Leafを指定でパスの末尾だけを取得出来る。
先頭の'PS 'は無くても良いけどPower Shellとわかるように一応つけている。

追記

改行パターン

function prompt {
    "${pwd}
[PS]> "
}

or

function prompt {
    $host.UI.WriteLine((Get-Location).Path);
    $host.UI.Write("[PS]");
    "> "
}

参考

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