Mark
Mark

Reputation: 5513

Windows Powershell $profile does not show a real path

I launch Windows Powershell (by hitting the windows key, typing "powershell" and pressing enter which launches C:\Windows\System32\WindowsPowerShell\v1.0) and type $profile and press enter and see WindowsPowerShell\Microsoft.PowerShell_profile.ps1

As far as I know, this is not a valid path. I was hoping for something like C:\Windows\...

When I type $profile | Format-List * -Force, however, there is some progress and I get

AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 50

However the CurrentUserAllHosts and CurrentUserCurrentHosts are still non-paths. What do these non-paths mean? Do they refer to some hidden values or do I need to set some system values somewhere?

Here is my $PsVersionTable.PsVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  206

Here are the results of Get-Host

Name             : ConsoleHost
Version          : 5.1.14393.206
InstanceId       : a2a61a42-f2ee-46b9-b67a-ef441301bdb8
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Upvotes: 26

Views: 12239

Answers (3)

mklement0
mklement0

Reputation: 440297

Note: This question is not about the $PROFILE file simply not existing, possibly along with its directory. If $PROFILE shows the expected path and you simply want to ensure that the file exists, use
if (-not (Test-Path $PROFILE)) { $null = New-Item -Force $PROFILE }

tl;dr:

The problem may not be PowerShell-related, but may be due to a missing special-folder path definition in the registry.

Verify that registry key HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders contains a REG_EXPAND_SZ value named Personal with data %USERPROFILE%\Documents - see diagnostic commands below.

If you find that you must (re)create it, use:

New-ItemProperty `
  'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' `
  Personal -Value '%USERPROFILE%\Documents' -Type ExpandString -Force

and then log off and back on (or reboot) to see if that fixed the problem.


Eris's helpful answer tells us that the user-specific PS profile paths are derived from what Environment.GetFolderPath(Environment.SpecialFolder.Personal) returns.

.NET gets this value, presumably via the SHGetKnownFolderPath Shell API function, from registry key HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders, value Personal, where it is normally defined as a REG_EXPAND_SZ value containing (expandable) string %USERPROFILE%\Documents.
(There's also a legacy fallback location - see here.)

Profiles CurrentUserAllHosts and CurrentUserCurrentHost containing just relative paths, namely:

  • WindowsPowerShell\profile.ps1
  • WindowsPowerShell\Microsoft.PowerShell_profile.ps1

suggests that the Environment.GetFolderPath(Environment.SpecialFolder.Personal) call, whose result is used as the path prefix, unexpectedly returned an empty string, which in turn suggests a registry problem.


Here are some diagnostic commands and their expected outputs (jdoe represents your username):

# Verify that %USERPROFILE% is defined.
> $env:USERPROFILE
C:\Users\jdoe

# Verify that %USERPROFILE% is defined in the registry.
> Get-ItemPropertyValue 'HKCU:\Volatile Environment' USERPROFILE
C:\Users\jdoe

# Verify that the API call to retrieve the known folder
# "Personal" (Documents) returns the expected value.
> [Environment]::GetFolderPath('Personal')
C:\Users\jdoe\Documents

# See if the registry contains the expected definition.
> Get-ItemPropertyValue 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' Personal
C:\Users\jdoe\Documents

Upvotes: 21

Hiznardo
Hiznardo

Reputation: 19

I had the same issue today and I was able to create the profile with the following PowerShell command:

New-Item -path $PROFILE -type File -force

I hope this helps!

Upvotes: 1

Eris
Eris

Reputation: 7638

According to the powershell source code on github, they look for Environment.SpecialFolder.Personal

It starts in ConsoleHost.cs and you can track this down to utils.cs where they call Environment.GetFolderPath(Environment.SpecialFolder.Personal);

Upvotes: 8

Related Questions