Skip to content

Instantly share code, notes, and snippets.

@RJ-Infinity
Created April 13, 2026 09:35
Show Gist options
  • Select an option

  • Save RJ-Infinity/b3112607d3ac3653c7ba5b4e3ff79dd8 to your computer and use it in GitHub Desktop.

Select an option

Save RJ-Infinity/b3112607d3ac3653c7ba5b4e3ff79dd8 to your computer and use it in GitHub Desktop.
a prompt for powershell copy this function into your $profile
function prompt {
$prompt_str = ""
$history = @(Get-History)
if($history.Count -gt 0)
{
$lastItem = $history[$history.Count - 1]
$lastId = $lastItem.Id
if ($LastExitCode -ne 0 -and $LastExitCode -ne $null) {
# we still cant trust this was acctualy the last commands error as it is only
# reset for native executables cmdlets dont touch $LastExitCode.
# slightly hacky but to fix we parse the last command and test if it is a native executable
$last_cmd = [System.Management.Automation.Language.Parser]::ParseInput(
$lastItem.CommandLine,
[ref]$null,
[ref]$null
).Find({
$args[0] -is [System.Management.Automation.Language.CommandAst]
}, $true).GetCommandName()
if ($last_cmd){
$last_cmd = Get-Command $last_cmd -ErrorAction SilentlyContinue
if ($last_cmd.CommandType -eq 'Application') {
$prompt_str += "Exited with ($LastExitCode)`n"
}
}
}
}
$nextCommand = $lastId + 1
$prompt_str += "PS7$Env:username:$(Get-Location)-$($nextCommand)-$>"
return $prompt_str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment