Changing prompt within a script

G

Garios

If a execute this piece in the PS console then a can change the default prompt:

function prompt {"PS:MyTest: $(get-location)>"}

But if a execute from within a script the new prompt doesn't stick. In other
words I want to be able to change my prompt dinamically as I execute a PS
script to reflect things that I am doing.
I don't want to put this in any profile to load at the beginning only, it
has to be as a execute a script.

Thanks,
 

My Computer

L

Larry__Weiss

Try

function global:prompt {"PS:MyTest: $(get-location)>"}

- Larry

On 4/30/2010 9:30 PM, Garios wrote:

> If a execute this piece in the PS console then a can change the default prompt:
>
> function prompt {"PS:MyTest: $(get-location)>"}
>
> But if a execute from within a script the new prompt doesn't stick. In other
> words I want to be able to change my prompt dinamically as I execute a PS
> script to reflect things that I am doing.
> I don't want to put this in any profile to load at the beginning only, it
> has to be as a execute a script.
>
> Thanks,
>
>
 

My Computer

T

Thomas Lee

In message <[email protected]>, Garios
<[email protected]> writes

>If a execute this piece in the PS console then a can change the default prompt:
>
>function prompt {"PS:MyTest: $(get-location)>"}
>
>But if a execute from within a script the new prompt doesn't stick.
Correct.

If you run this within a script, it 'sticks' while the script is running
then, since it was defined in the script scope it disappears.

If you have this function definition in f.ps1, then typing:

PSH [C:\foo]> .\f.ps1

results in nothing.

you could try:
PSH [C:\foo]> . .\f.ps1

That's a dot-source of the function.

>In other
>words I want to be able to change my prompt dinamically as I execute a PS
>script to reflect things that I am doing.
>I don't want to put this in any profile to load at the beginning only, it
>has to be as a execute a script.
Use dot sourcing.

Thomas
--
Thomas Lee
[email protected]
 

My Computer

L

Larry__Weiss

On 5/22/2010 9:50 AM, Thomas Lee wrote:

> In message <[email protected]>, Garios
> <[email protected]> writes

>> If a execute this piece in the PS console then a can change the
>> default prompt:
>>
>> function prompt {"PS:MyTest: $(get-location)>"}
>>
>> But if a execute from within a script the new prompt doesn't stick.
>
> Correct.
>
> If you run this within a script, it 'sticks' while the script is running
> then, since it was defined in the script scope it disappears.
>
> If you have this function definition in f.ps1, then typing:
>
> PSH [C:\foo]> .\f.ps1
>
> results in nothing.
>
> you could try:
> PSH [C:\foo]> . .\f.ps1
>
> That's a dot-source of the function.
>

>> In other
>> words I want to be able to change my prompt dinamically as I execute a PS
>> script to reflect things that I am doing.
>> I don't want to put this in any profile to load at the beginning only, it
>> has to be as a execute a script.
>
> Use dot sourcing.
>
> Thomas

In case the script for other reasons is not appropriate for
dot-sourcing,
the global prompt function scope can be accessed via syntax like

function global:prompt {"PS:MyTest: $(get-location)>"}

Does anyone know how to specify the parent or grandparent scope
for the function name space? I looked and could not find it.

- Larry
 

My Computer

Top