VMware Cloud Community
Tocano
Enthusiast
Enthusiast
Jump to solution

Set starting directory in PowerCLI

So this may be a bit pedantic, but I'm sure others understand how you start trying to do something simple and it snowballs into a much larger issue.

Well, I wanted to set the default directory I start in when I launch PowerCLI instead of the default C:\. I started with adding a 'cd C:\path\' in a generic Powershell profile file. However, this didn't work.

But my normal Powershell consoles were starting in the correct path, so I figured it must be something happening with PowerCLI specifically. So I looked in 'Initialize-PowerCLIEnvironment.ps1' and saw a 'cd \' around line 269 (PowerCLI 6.0R3). This appeared to be the culprit.

In accordance with the documentation, instead of modifying the default Initialize file and possibly messing up the signature, I then tried to create a custom 'Initialize-PowerCLIEnvironment_Custom.ps1' file and added 'cd C:\path\' to that. According to the documentation, "The application recognizes and loads the custom file after loading the default script configuration file."  However, again, this didn't work.

Looking back at the default Initialize script, I noticed that the 'cd \' is actually located AFTER it runs the custom init script (lines 264-267).

So it seems this 'cd \' is pretty much the LAST thing that will run before you get a prompt. So the only option I could find, despite the documentation's warning, was to modify the default Initialize script.

Is there a more elegant approach to this I'm overlooking? Why does this set a default directory at all - especially at that point in the initialization? This almost feels like a development line that was supposed to be removed before publishing, but was overlooked.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The trick is in the order of things that are executed.

Your PowerShell profile is executed when the PowerShell session is started, so the PowerCLI initialisation scripts comes behind, overriding your CD

What I do is the following:

  • don't start PowerCLI from the shortcut on the Desktop or in the Start menu
  • start PowerShell or PowerShell ISE
  • in my User profile I call the PowerCLI initialisation script
  • and after the call to this script I do my own CD (in my case to my scripts folder)

If you want to be able to initialise the PowerCLI environment from within a running PowerShell session, you could define a function in your PS profile.

Mine is called Start-PCLI, and I do a Push-Location and Pop-Location before and after the call to the initialisation script.

That way I will stay in the current folder.

function Start-PCLI

{

    Push-Location

    & "${env:ProgramFiles(x86)}\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

    Pop-Location

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

7 Replies
jpsider
Expert
Expert
Jump to solution

So here is what I do at the beginning of my scripts to load my custom cmdlets.

$MYINV = $MyInvocation

$SCRIPTDIR = split-path $MYINV.MyCommand.Path

# Enables all of the needed cmdlets

. "${SCRIPTDIR}\device-cmdlets.ps1"

. "${SCRIPTDIR}\mysql-cmdlets.ps1"

Here is a link to my stuff, if you want to take a closer look at the rest of myscript/directory structure.

BelayTechnologies/Belay-Device · GitHub

-Justin

Reply
0 Kudos
Tocano
Enthusiast
Enthusiast
Jump to solution

That's a good way to start scripts and set the current working directory. However, it doesn't seem to address setting the default working directory (at startup) of a PowerCLI console as I was referring to.

I'll look at some of your scripts. Thanks for sharing.

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

If you create a desktop shortcut you can change the target directory. That should place you in a directory of your choice for start up of powercli.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The trick is in the order of things that are executed.

Your PowerShell profile is executed when the PowerShell session is started, so the PowerCLI initialisation scripts comes behind, overriding your CD

What I do is the following:

  • don't start PowerCLI from the shortcut on the Desktop or in the Start menu
  • start PowerShell or PowerShell ISE
  • in my User profile I call the PowerCLI initialisation script
  • and after the call to this script I do my own CD (in my case to my scripts folder)

If you want to be able to initialise the PowerCLI environment from within a running PowerShell session, you could define a function in your PS profile.

Mine is called Start-PCLI, and I do a Push-Location and Pop-Location before and after the call to the initialisation script.

That way I will stay in the current folder.

function Start-PCLI

{

    Push-Location

    & "${env:ProgramFiles(x86)}\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

    Pop-Location

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tocano
Enthusiast
Enthusiast
Jump to solution

and after the call to this script I do my own CD (in my case to my scripts folder)

Doesn't that sort of defeat the purpose of the previous steps?

Thanks for the recommendation on push-/pop-location. I was not familiar with them. Though, this whole thing feels like a bit of unnecessary effort to set one's default directory.

I still feel like line 269 should be removed from the default Initialization script - or at least relocated to much earlier in the script.

Using push-/pop-location does seem to provide a better option than modifying the default Initialization file. However, this is not as simple in a multi-user environment.

Anyway, thank you for the suggestion.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That depends, if my PowerShell session is already in the right directory, I don't do anything after I call the function.

In fact, in my PowerShell profile I change to my Scripts folder.

But for some projects I might work in another folder.

On the question of the cd in the initialisation script, yes, you are right, that could be removed.

But this initialisation script is used by many. And perhaps some users depend on finding themselves in that location after the initialisation script.

If the PowerCLI Team removes it, it would be a change in the behaviour of PowerCLI.

On the multi-user environment, in PowerShell there are multiple profiles possible.

Each user can define his own working directory, or you set one for all users on the same station.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Tocano
Enthusiast
Enthusiast
Jump to solution

True, and I'll play with the profiles. Thank you.

If the PowerCLI Team removes it, it would be a change in the behaviour of PowerCLI.

I feel like this is already a change from previous behavior (this 'cd \' line is NOT in previous versions of this script until 6.0R2). And if one wanted to avoid changing the current behavior, the 'cd \' could simply be moved a dozen lines earlier (before the custom initialize script inclusion) and not really change behavior while allowing customization to override it.

Anyway, I have what I need to work around it. Thank you. I'll stop ranting. Smiley Happy

Reply
0 Kudos