Adjust prompt function in PowerShell

Today I want to share with you my experience about how to adjust prompt function in PowerShell.

I’ve created prompt function which display details about current user session.
It shows details about followind things:
– Hours – display time of last executed command
– Computer name – name of the computer on which powershell session is open
– User name – name of the user which is currently using Powershell session.
At the beginning of the function I’ve added also if condition which will check if session is run with admin privileges.
If yes, this infromation will be added to propmpt bar.

Function:

function prompt
{
    if([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544"))
    {
        $Role = "(Admin)"
    }
    $Hours = (get-date).Tostring("HH:mm:ss")
    
    Write-Host $Hours -NoNewline -ForegroundColor Green
    Write-Host "|" -NoNewline
    Write-Host "PS@$env:COMPUTERNAME" -NoNewline -ForegroundColor Magenta
    Write-Host "|" -NoNewline
    Write-Host $env:USERNAME$Role -NoNewline -ForegroundColor Gray
    Write-Host "|" -NoNewline
    Write-Host $(get-location) -nonewline -ForegroundColor Cyan
    "`n$('>_') "
    return " "
}

In result you should have prompt like on below screen (of course with your computer and account details).

To have this prompt included in every run of Powershell you should adjust your Powershell profile.
If you want to do it go to howtogeek.com blog and use tips from this site.

I hope it will be usefull for some of you 😉
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.