VMware Cloud Community
will47
Contributor
Contributor

Overriding PowerCLI's prompt function in profile.ps1

Is there a way to override or modify the default 'prompt' function that PowerCLI sets? It seems to get sourced after profile.ps1, so even if you set a different prompt function there, it gets overridden.

Of course, you can type

function prompt "PS $(Split-Path $pwd -leaf)>" or whatever after logging in, but I'm wondering how to do this automagically.

0 Kudos
3 Replies
mattboren
Expert
Expert

Hello, will47-

There sure is.  Sounds like you are launching PowerShell via the PowerCLI shortcut or menu item that results from the PowerCLI install.  If so, you can edit the script file referenced in that the shortcut, which is currently something similar to "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1".

This script file defines the prompt() function.  You can modify it there as you wish, or comment it out so that the prompt() function you have defined in your profile.ps1 gets used.

will47
Contributor
Contributor

I am using that shortcut, but don't want to mess up the setup for other users on the system.

Maybe it's easier to just not use the shortcut and load the PowerCLI cmdlets from within my profile?

Edit: I will probably go that route, but after reading the initialization script, it looks like they save the original prompt to a variable, so that you can set it back:

# Modify the prompt function to change the console prompt.
# Save the previous function, to allow restoring it back.
$originalPromptFunction = $function:prompt

So you could probably also do:

$function:prompt = $originalPromptFunction

or something like that in your profile.

Thanks!

0 Kudos
mattboren
Expert
Expert

Hello, will47-

Yes, not using the shortcut and then adding the PowerCLI PSSnapins directly in your profile is a way to go.  There are some useful functions/aliases included in that PowerCLI initialization script, as well, though.  So, you might consider grabbing those as well, or editing the PowerCLI initialization script, or, like you said, just "restoring" the prompt function via the $originalPromptFunction variable that gets set.

0 Kudos