👻

PowerShellのPrompt関数 (Ubuntu風)

2021/07/28に公開

画像のようなPowerShellのプロンプトを表示する関数です。

function Prompt { 
	$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
	$principal = [Security.Principal.WindowsPrincipal] $identity
	$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator

	[bool] $isDebug = (Test-Path variable:/PSDebugContext)
	[bool] $isAdmin = ($principal.IsInRole($adminRole))
	[string] $userName = $env:USERNAME
	[string] $computerName = $env:COMPUTERNAME
	[string] $path = Split-Path (Get-Location) -Leaf

	if ($isDebug) {
		Write-Host "[DGB]" -NoNewline -ForegroundColor Red
		Write-Host ":" -NoNewline -ForegroundColor White
	}
	Write-Host "$userName@$computerName" -NoNewline -ForegroundColor Green
	Write-Host ":" -NoNewline -ForegroundColor White
	Write-Host "$path" -NoNewline -ForegroundColor Blue
	if ($isAdmin) {
		Write-Host "#" -NoNewline -ForegroundColor White
	} else {
		Write-Host "$" -NoNewline -ForegroundColor White
	}

	return " "
}

Discussion