Created
April 13, 2026 09:35
-
-
Save RJ-Infinity/b3112607d3ac3653c7ba5b4e3ff79dd8 to your computer and use it in GitHub Desktop.
a prompt for powershell copy this function into your $profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 += "[31mExited with ($LastExitCode)[0m`n" | |
| } | |
| } | |
| } | |
| } | |
| $nextCommand = $lastId + 1 | |
| $prompt_str += "PS7[32m$Env:username[0m:[36m$(Get-Location)[33m-$($nextCommand)-[96m$[93m>[0m" | |
| return $prompt_str | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment