Hello Everyone,

A lot of customers I work with often ask a question on how to display line numbers within the default PowerShell Console.Out-of-the-box, PowerShell does not display the line numbers.However, you can always tweak it to show the line numbers something similar to what Bruce Payette uses in his book “Windows PowerShell in Action, Second Edition” published by Manning.

In order to do this, we will follow a two step process

  • Create a PowerShell Profile that contains the code to append line numbers
  • Ensure that the PowerShell Profile is loaded by setting the appropriate Execution Policy

Create a PowerShell Profile that contains the code to append line numbers

Navigate to your Documents Directory and create a folder called “WindowsPowerShell” as shown in the screenshot below:

mydoc

Now fire up Notepad or your favourite Script Editor (I love PowerGUI Pro) and type in the following lines of PowerShell Code:

   1: function prompt
   2: {
   3:      $global:LINE=$global:LINE + 1
   4:     'PS ' + $(Get-Location) + ' ' + '(' + $($global:LINE) + ')' + $(if ($nestedpromptlevel -ge 1) { '>>' }) + ' > '
   5: }

Save the file as “Profile.ps1” within the WindowsPowerShell Directory you created in the step above.

image

Check the Execution Policy by running the following command

   1: Get-ExecutionPolicy

I normally set the Execution Policy for my PowerShell environment as RemoteSigned. If you do this in a Production Environment, it is highly recommended that you set the Execution Policy as AllSigned.

To set the ExecutionPolicy as RemoteSigned, you can use the Command-let Set-ExecutionPolicy. Make sure you run this command-let in the elevated PowerShell console. In my Lab environment, I tend to use the UnRestricted Policy as shown in the screenshot below:

image

Now, close all the PowerShell consoles opened in your session and re-launch PowerShell. PowerShell will automatically scan your profile.ps1 and load it. That’s it! As you see in the screenshot below, you now have line numbers in your PowerShell console.

final

Happy Scripting!