0
0

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 3 years have passed since last update.

PowerShellのプロンプトの視認性を向上させる

Last updated at Posted at 2021-12-29

やりたいこと

PowerShellのデフォルトのプロンプトは、カレントフォルダのパスが長くなると視認性が著しく低下するため、下記のように変更する。

  • コマンド行を2行に分割する(1行目にカレントフォルダを表示、2行目をコマンド行にする)
  • コマンドの実行後に改行を挟む

デフォルトのプロンプト

PS C:\Users\*****> "hoge"
hoge
PS C:\Users\*****> "fuga"
fuga
PS C:\Users\*****>

変更後のプロンプト

PS C:\Users\*****
> "hoge"
hoge

PS C:\Users\*****
> "fuga"
fuga

PS C:\Users\*****
>

プロンプトをカスタマイズする

prompt関数を上書きすることでプロンプトの表示をカスタマイズできる。

prompt関数を確認する

関数の情報はGet-Itemコマンドレットで取得でき、
Definitionプロパティで関数の内容を確認できる。

> (Get-Item function:prompt).Definition

"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

プロファイルを編集する

プロファイルに同名の関数を定義することで、デフォルトの関数を上書きすることができる。
プロファイルのパスは$profileで確認できる。

> $profile
C:\Users\*****\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Microsoft.PowerShell_profile.ps1
function prompt { "`r`nPS " + $(Get-Location) + "`r`n> " }

プロファイルを適用する

PowerShellを再起動するとプロファイルが適用される。

prompt関数が上書きされていることを確認

> (Get-Item function:prompt).Definition
 "`r`nPS $(Get-Location)`r`n> "

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?