Refactored Codes
Modify prompt to show line number in powershell in windows
I make youtube videos, so, I thought it would be good if line numbers are shown in the terminal. That way, I will able to refer the already executed commands, by line numbers. To do that, you have to add or modify the prompt function in your powershell profile. Below is the definition of the prompt function.
[int]$promptLineNumber = 0;

function prompt {
 ++$global:promptLineNumber;
 "$($executionContext.SessionState.Path.CurrentLocation) [$promptLineNumber] $('>' * ($nestedPromptLevel + 1)) ";
}
The above function will show the line number, right after the directory. As, shown in the below image. It is time for some explaination. When powershell terminal starts, it runs few scripts. And, path to those scripts are present in $PROFILE object. To check for the presence of file, run the below command.
TEST-PATH $PROFILE
If profile script is not present, you can use below mentioned command to create it.
New-Item -ItemType File -Path $PROFILE
Open the profile in any text editor, and add prompt function at the end of the file. To reload the current powershell session scripts. Use the dot-operator.
. $PROFILE
This will run the entire script again and load the session with current content of $PROFILE. In the script, i have declared a variable to keep the count of current line number.
[int]$promptLineNumber = 0;
To increment and access the variable, we use global scope. Because, the variable created in profile script is put into global scope.
++$global:promptLineNumber;
And, the next change, we put the line number in the final prompt string.
"$($executionContext.SessionState.Path.CurrentLocation) [$promptLineNumber] $('>' * ($nestedPromptLevel + 1)) ";
This is not a complex programming, we just need one variable, which gets incremented everytime new prompt is displayed. And, the value of the line number is added in the final prompt string.