How to remove the "username@host" from powershell prompt?
Asked Answered
C

4

7

I am using Windows Terminal(Preview) with shells like 1.PowerShell, 2.WSL in my Windows 10 machine. I installed the latest version of oh-my-posh and posh-git to customize the terminal. My current theme is Agnoster which gives a colorful custom prompt. But I want to get rid of the "username@host" from my prompt.

Eg:

Current => username@host D:\folder-name>

Needed => D:\folder-name>

I tried few things with $GitPromptSettings variable and also in GitPrompt.ps1 file which is inside the posh-git folder but with no use.

Also, since i have oh-my-posh and posh-git, does both have prompt customization properties or it is only from posh-git?

Any help is appreciated.

Candless answered 5/3, 2020 at 11:28 Comment(0)
T
8

remove the segment which type is session from ~\Documents\WindowsPowerShell\Modules\oh-my-posh\3.101.23\themes\<themename>.omp.json

Tica answered 22/3, 2021 at 3:50 Comment(0)
L
7

You have to set $DefaultUser before importing modules.

Example:

$global:DefaultUser = [System.Environment]::UserName
Import-Module posh-git 
Import-Module oh-my-posh
Set-Theme Paradox
Leatherjacket answered 8/3, 2020 at 7:56 Comment(2)
OMG. Awesome. Thanks. It worked. But can you please explain what we are doing and how it is working? Because, i was thinking that, i should do something on the GitPrompt variable which is returned by posh.gitCandless
This is no longer working in Oh My Posh 3, see the answer by @Tica below: https://mcmap.net/q/1399066/-how-to-remove-the-quot-username-host-quot-from-powershell-promptSempstress
S
6

In Oh My Posh 3, the method for hiding the username@host part has changed because the whole theme configuration has changed.

The username@host portion of the powerline comes from the "session" segment. The configuration options for session can be found here: https://ohmyposh.dev/docs/session

Interesting options are:

  • display_user: boolean - display the user name or not - defaults to true
  • display_host: boolean - display the host name or not - defaults to true
  • default_user_name: string - name of the default user - defaults to empty
  • display_default: boolean - display the segment or not when the user matches default_user_name - defaults to true

And the note on environment variables:

POSH_SESSION_DEFAULT_USER - used to override the hardcoded default_user_name property

Solution

Based on this, I made the following changes:

  1. For the theme, I added the display_default: false property to the "session" segment:
    {
      "type": "session",
      // ....
      "properties": {
        "display_default": false
      }
    }```
    
  2. In my powershell profile, I set the new environment variable to my own username: $env:POSH_SESSION_DEFAULT_USER = [System.Environment]::UserName

This makes the user@host part of the prompt disappear as long as I'm using my default username.

Sempstress answered 15/4, 2021 at 7:25 Comment(1)
It's not clear from your answer wether you need to do 1. and 2. or just either one of them. The default location of the theme files could also be mentioned.Brandenbrandenburg
L
2

You may check {theme's name}.psm1 in the Themes/ directory to understand how it works. The key is the following code.

e.g. Paradox.psm1

$user = $sl.CurrentUser
....
if (Test-NotDefaultUser($user)) {

        $prompt += Write-Prompt -Object "$user@$computer " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor

}

if $DefaultUser is same to $user or is different $null, your powershell does not show $user@$computer.

Leatherjacket answered 10/3, 2020 at 2:30 Comment(1)
Thanks. Actually I was going through the Agnoster.psm1 file to understand the logic. Ii will be easy if i could debug the file. Seems there is a way to debug. Let me check. Thanks Again.Candless

© 2022 - 2024 — McMap. All rights reserved.