I am trying to customize my PS Prompt in Windows 10 by adding the Azure Account ID. However, it is not working as expected.
Output of Azure Account ID, results in the following:
[0.04 sec] > (Get-AzContext).Account.Id
[email protected]
I want the my result 'Account.Id' from the above command should be displayed in my PS Prompt. But it is not displaying the necessary results. Please refer the below screenshot for information.
Screenshot of the Current PS Prompt:
Microsoft.PowerShell_profile.ps1
function prompt {
#Assign Windows Title Text
$host.ui.RawUI.WindowTitle = "Current Folder: $pwd"
#Configure current user, current folder and date outputs
$CmdPromptCurrentFolder = Split-Path -Path $pwd -Leaf
$CmdPromptUser = [Security.Principal.WindowsIdentity]::GetCurrent();
$Date = Get-Date -Format 'dddd hh:mm:ss tt'
# Test for Admin / Elevated
$IsAdmin = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
#Calculate execution time of last cmd and convert to milliseconds, seconds or minutes
$LastCommand = Get-History -Count 1
if ($lastCommand) { $RunTime = ($lastCommand.EndExecutionTime - $lastCommand.StartExecutionTime).TotalSeconds }
if ($RunTime -ge 60) {
$ts = [timespan]::fromseconds($RunTime)
$min, $sec = ($ts.ToString("mm\:ss")).Split(":")
$ElapsedTime = -join ($min, " min ", $sec, " sec")
}
else {
$ElapsedTime = [math]::Round(($RunTime), 2)
$ElapsedTime = -join (($ElapsedTime.ToString()), " sec")
}
#Decorate the CMD Prompt
Write-Host ""
Write-Host "Azure: (Get-AzContext).Account.Id"
Write-host ($(if ($IsAdmin) { 'Elevated ' } else { '' })) -BackgroundColor DarkRed -ForegroundColor White -NoNewline
Write-Host " USER:$($CmdPromptUser.Name.split("\")[1]) " -BackgroundColor DarkBlue -ForegroundColor White -NoNewline
If ($CmdPromptCurrentFolder -like "*:*")
{Write-Host " $CmdPromptCurrentFolder " -ForegroundColor White -BackgroundColor DarkGray -NoNewline}
else {Write-Host ".\$CmdPromptCurrentFolder\ " -ForegroundColor White -BackgroundColor DarkGray -NoNewline}
Write-Host " $date " -ForegroundColor White
Write-Host "[$elapsedTime] " -NoNewline -ForegroundColor Green
return "> "
} #end prompt function
CodePudding user response:
Tomalak is hinting at the problem in a comment (and shows an alternative solution), but let me spell it out:
In order to:
embed the output from an expression or command -
(Get-AzContext).Account.Id
, in your case - inside"..."
,an expandable (double-quoted) string,you need to enclose it in
$(...)
, the subexpression operator:
Write-Host "Azure: $((Get-AzContext).Account.Id)"
See this answer for a comprehensive overview of PowerShell's expandable strings (string interpolation).
In general, only the following characters are metacharacters inside "..."
- all others are treated as literals (which explains why you saw verbatim (Get-AzContext).Account.Id)
in your prompt string):
`
(backtick) PowerShell's escape character - see the conceptual about_Special_Characters help topic.- If doubled, also
"
:""
escapes a single"
and is equivalent to`"
- If doubled, also
$
, the start of a variable reference (e.g.,$HOME
) or subexpression ($(...)
, as above).
For the sake of completeness, here are alternative solutions:
# String concatenation - the expression must be enclosed in (...)
Write-Host ('Azure: ' (Get-AzContext).Account.Id)
# The -f (string-formatting operator)
Write-Host ('Azure: {0}' -f (Get-AzContext).Account.Id)
# Tomalak's alternative, which relies on Write-Host space-concatenating
# its individual arguments.
# Note how a single expression can act as a command argument as-is.
Write-Host 'Azure:' (Get-AzContext).Account.Id