Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Powershell $profile does not show a real path

Tags:

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 
like image 542
Mark Avatar asked Oct 01 '16 17:10

Mark


People also ask

What is $Profile PowerShell?

The $PROFILE automatic variable stores the paths to the PowerShell profiles that are available in the current session. To view a profile path, display the value of the $PROFILE variable. You can also use the $PROFILE variable in a command to represent a path.

Where is PowerShell 7 profile?

The path to the location of the profile has changed in PowerShell 7. In Windows PowerShell 5.1, the location of the profile is $HOME\Documents\WindowsPowerShell . In PowerShell 7, the location of the profile is $HOME\Documents\PowerShell .


2 Answers

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 
like image 98
mklement0 Avatar answered Sep 30 '22 17:09

mklement0


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);

like image 30
Eris Avatar answered Sep 30 '22 18:09

Eris