Works on command line but not in $PROFILE?

I’ve been banging my head against the wall on this one. So if anyone can help my I’d be very happy!

I want to add the path to my local modules via my $PROFILE. The command I want to use looks as this:

$env:PSModulePath += ‘;’+ (Join-Path -Path ${env:HOME} -ChildPath ‘Documents\WindowsPowerShell\Modules’)

What bugs me is that this command works fine when i run it from the command line, i.e. the “documents\WindowsPowerShell\Modules” location is added to the $env:PSModulePath.

but when I add the very same line to my $PROFILE it does not work. I get the error:

Join-Path : Cannot bind argument to parameter ‘Path’ because it is null.
At C:\Users\Joakim\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:3 char:44

  • $env:PSModulePath += ‘;’+ (Join-Path -Path ${env:HOME} -ChildPath 'Do …
  •                                        ~~~~~~~~~~~
    • CategoryInfo : InvalidData: (:slight_smile: [Join-Path], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

Is there something special about how the $PROFILE can or can not access environment variables? Or can any of you guys see what misstake I have made?

It’s likely just not populated at that point. You might try using the ENV: drive instead of the variable.

Or, Why not just permanently modify the environment variable in windows itself?

Ok, I could do that… but my intention with setting up the modulepath in the profile is to not make anything machine specific. I did find something interesting.

If I replace ${env:HOME} with ${env:USERPROFILE} everything works. So now I am really curious, why is the environment variable USERPROFILE available but not the HOME variable? Weird.

I have no HOME environment variable on my system. There’s an automatic $home variable in PowerShell, and as you already pointed out, there’s also a USERPROFILE environment variable.

Thanks Dave, as I updated my post to say, I ended up using the $env:USERPROFILE variable which is a System.Environment variable and therefore is available also when the PowerShell profile is executing.

All I was pointing out is that there’s a difference between $env:home and $home. On my computer, $env:home never exists, but $home works just like $env:USERPROFILE (regardless of when you use it; works fine in a profile as well.)

Edit: On a side note, the $home variable is initialized from the values of $env:HOMEDRIVE and $env:HOMEPATH , which may or may not be the same as $env:USERPROFILE (depending on how the Profile tab of your account is set up.)