Move flashing prompt sign away from left edge of PowerShell window

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Move flashing prompt sign away from left edge of PowerShell window

#1 Post by DOSadnie » 14 Sep 2023 07:35

Is there a way to move the prompt sign in PowerShell [an also CMD] window more toward middle / right side of a it? So that a new empty line would flash it prompt sign as not being adjacent to the left edge of the window when awaiting for input from user?

This almost does what I need

Code: Select all

# -The countdown function
function Start-Countdown {
    $Countdown = 2
    while ($Countdown -gt 0) {
        Write-Host ("Countdown: $Countdown seconds") -NoNewline
        Start-Sleep -Seconds 1
        $Countdown--
        Write-Host "`r" -NoNewline
    }
}

# Start the countdown
Start-Countdown

# Clear the countdown line
Write-Host "`r"

# Modifications of prompt
$Prompt = " " * 2 + ""
Write-Host $Prompt -NoNewline

# Wait for input from user
Read-Host ""
as it shows
ppp:p_
instead of
pppp_
[where each >>p<< represent a white pause]

Where is that splitting >>:<< sign coming from? And more importantly: how to get rid of it?

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#2 Post by DOSadnie » 21 Sep 2023 01:46

I have managed to co come up with a partial solution

This script

Code: Select all

$COUNTDOWN = 3

Write-Host
Write-Host "   Endng in"
Write-Host
Write-Host "   $COUNTDOWN"
Write-Host
Write-Host "   seconds"
Write-Host

Start-Sleep -Seconds $COUNTDOWN
 
Write-Host
Write-Host
Write-Host "   Script has been executed"
Write-Host

Write-Host "`r"   # This clears the line

$Prompt = " " * 2 + " "   # That separate single white space at the end of this line can be entirely removed or changed into whatever sign or text is desired
Write-Host $Prompt -NoNewline

Read-Host
will leave user with the third / last prompt line / sign being moved away from the left edge of the window - but so far I have been unsuccessful with making look like so all of the prompt signs [the initial one and the second during the countdown in this new example from this post]

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#3 Post by DOSadnie » 22 Oct 2023 09:02

Does anyone have any ideas about how to do this?

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Move flashing prompt sign away from left edge of PowerShell window

#4 Post by ShadowThief » 22 Oct 2023 09:30

Are you just looking for prompt $S$S$P$G?

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#5 Post by DOSadnie » 18 Nov 2023 11:45

If you mean replacing line

Code: Select all

$Prompt = " " * 2 + " "
with either

Code: Select all

$Prompt = "$S$S$P$G"
$Prompt = $S$S$P$G
$Prompt $S$S$P$G
then this does not work

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#6 Post by DOSadnie » 28 Dec 2023 03:24

DOSadnie wrote:
18 Nov 2023 11:45
If you mean replacing line
[...]
Well, did you?

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#7 Post by DOSadnie » 18 Feb 2024 03:39

I made basic tests files with just

Code: Select all

prompt $S$S$S$P$G
Read-Host

Code: Select all

function prompt {
    "$(' ' * 3)$(Get-Location)> "
}
Read-Host

Code: Select all

function prompt {
    Write-Host (" " * 3) -NoNewline
    return " $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
}
Read-Host
but they all were showing to me the prompt sign adjacent next to left edge of PS window


How else can I try to move the prompt to the right?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Move flashing prompt sign away from left edge of PowerShell window

#8 Post by Squashman » 18 Feb 2024 09:00

Seems to work just fine for me.

Code: Select all

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\Squashman> function prompt {Write-Host (" " * 10) -NoNewline}
          PS>function prompt {Write-Host (" " * 15) -NoNewline}
               P> function prompt {Write-Host (" " * 20) -NoNewline}
                    PS>
 

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#9 Post by DOSadnie » 22 Jun 2025 07:11

If I open PowerShell console as Administrator and paste in

Code: Select all

function prompt {Write-Host (" " * 3) -NoNewline}
and press Enter, then I do get what I want i.e. the prompt line moved 3 space signs away from the left edge of that window

But if I will save it as a PS1 file

Code: Select all

function prompt {Write-Host (" " * 3) -NoNewline}
Read-Host
and execute it, then the PowerShell window will show the prompt sign in its default position i.e. adjacent to left edge

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#10 Post by DOSadnie » 22 Jun 2025 08:32

I did some further tests. And unfortunately when trying to come up with a workaround utilizing re-opening of PowerShell window and / or temporary file, I stumbled upon the same issue as with my other script [viewtopic.php?f=3&t=12105&p=71349#p71346]: the inability to stop HIPS of COMODO from asking for the same permission every time I execute it, despite green-lighting it and telling it to remember my choice

So this issue must be dealt with within one file and one console window

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#11 Post by DOSadnie » 23 Jun 2025 06:26

[The below is something that should rather be struck from the record - because it is most likely not true and to which current claim attest my later findings]

So far I have a limited success:

if I put this at the beginning of multi-step / multi-prompt script

Code: Select all

$global:NextPromptPrefix = ''

function prompt {
    if ($global:NextPromptPrefix) {
        $prefix = $global:NextPromptPrefix
        $global:NextPromptPrefix = ''
    } else {
        $prefix = ''
    }
    return "$prefix$((Get-Location).Path)> "
}

function Set-NextPromptPrefix {
    param([string]$prefix)
    $global:NextPromptPrefix = $prefix
}
then if I put this also this at the end of an section

Code: Select all

Set-NextPromptPrefix 'THIS TEXT WILL NOT BE DISPLAYED DUE TO WHATEVER GLITCH IN THE CODE - BUT IT NEVERTHELESS CAUSES SOMETIMES THE DESIRED BEHAVIOR OF THE PROMPT'
the new line sometimes inherits the position of the prompt from the previous one [and the position in previous one is, in most cases, a setting can more or less control]

So either this must be polished - or a different approach is needed

DOSadnie
Posts: 160
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Move flashing prompt sign away from left edge of PowerShell window

#12 Post by DOSadnie » 22 Jul 2025 04:21

All in all I needed to implement an A-OK working solution to my already written multi-step PS1 script that had a purpose of generating a report of what it was doing at a given moment. And the main visual issue kept coming back - which was seeing sometimes the prompt sign still being adjacent to the left edge of window of console for an annoying split of second

And after having wasted hours I can say this: it quickly became a futile game of Whac-A-Mole. Here is in short how it went down:


I thought I was finished when I ended up with two semi-working snippets

Code: Select all

Write-Host "`r"   # This clears the line
$A_VARIABLE = @()
# The below modifies the prompt with extra spaces
$THE_PROMPT_SIGN_VISUAL_MODIFICATION = " " * 2 + " "   # That separate single white space at the end of this line can be entirely removed or changed into whatever sign or text is desired
Write-Host $THE_PROMPT_SIGN_VISUAL_MODIFICATION -NoNewline
Start-Sleep -Seconds 1   # Timeout for providing a time needed for visual checking of position of the prompt
and

Code: Select all

$CURVLINEAR_COORDINATE = $host.UI.RawUI.CursorPosition   # This gets current position of the prompt
# The below moves the position of the prompt relative to the previous one
$host.UI.RawUI.CursorPosition = @{
    X = $CURVLINEAR_COORDINATE.X + 3   # Horizontal movement
    Y = $CURVLINEAR_COORDINATE.Y   # Vertical movement
}
Start-Sleep -Seconds 1   # Timeout for providing a time needed for visual checking of position of the prompt
I decided to use them as a function because there was no observable difference between using the 2 above methods directly and calling them via a function. Only rarely one method seemed to work slightly better than the other in a given segment of the file [i.e. moved the prompt immediately, concealed the prompt, did not interact negatively with the following lines of code] but basically these two yielded the same result. [I also have noticed that some parts of the file containing simpler code made the prompt sign to have apparently to little time to flash itself at all- and thus to flash itself in a wrong position. And adding a timeout there was allowing for the prompt to manifest itself in a possibly wrong position]

But nevertheless I had to polish usage of the above with precisely placed multiple instances of

Code: Select all

Write-Host " `b"

Code: Select all

Write-Host " `0"  

Code: Select all

[console]::SetCursorPosition(0, [console]::CursorTop - 1)
and / or removal of already existing lines of empty text that were present in my overall code [for upkeeping of the visual design of the report]. And like I said, it was like a game of Whac-A-Mole. The hardest part was adjustment of the dynamic countdown effect

And then as a last resort I introduced into the file the

Code: Select all

[System.Console]::CursorVisible = $false
And as it came to be, such simple turning off the prompt rendered that whole shebang pretty much futile; i.e. it would have been probably a much simpler and quicker to use this command right at the very beginning of code and from the get-go - and then to turn it back on only for desired segments that are visible later on in console, which in turn would allow for having to deal with position of the prompt [also] only in those desired segments of the code [instead of polishing every single segment]

But for now I am keeping usage of my function and all of those on-the-spot adjustments, because removal of them would require another time consuming set of tests needed for restitution of old visuals of lines being drawn in console and addition of new corrections to them. But of course there must be a significant drawback using this miraculous latest polishing method, because Whac-A-Mole is not over: now the countdown looks like it is slowed down by ~100% [i.e. the reported supposed seconds last way too long]

But I will leave it as it is right now

Post Reply