Author Posts

July 24, 2014 at 7:25 am

I am new to powershell, I have a background in k-shell/bash. Where are the default profile/environment variables physically stored? In bash everything is stored in .profile and there is no need to create your profile file. I echo $home & $profile and get values for each, but I am not seeing a profile file anywhere! When I look up the value of PSModulePath under system variables that value is not even matching what I echo for $psmodulepath in powershell? What's going on here? Where are they coming from? They have to be stored somewhere?

July 24, 2014 at 7:35 am

There are a few different concepts in play here:

Environment Variables, such as PSModulePath, have a few different "scopes" (for lack of a better term). There are System environment variables that apply to all users, User environment variables that apply to all processes for a particular user, and Process environment variables which are set at runtime, but not saved. When you start a new process, you inherit values from all three sets (including Process variables from whatever parent process launched the new one.)

$profile is a variable which contains several paths to scripts that PowerShell will execute on startup if they exist, but the folders and scripts themselves do not exist by default. If you want a profile, you have to create it. For example, this bit of code will create a profile.ps1 script (which executes for both PowerShell.exe and the ISE) for the current user, if it doesn't already exist, then opens that profile script in the ISE:

$profilePath = $profile.CurrentUserAllHosts
$profileFolder = Split-Path $profilePath -Parent

if (-not (Test-Path $profileFolder -PathType Container))
{
    $null = mkdir $profileFolder
}

if (-not (Test-Path $profilePath -PathType Leaf))
{
    $null = New-Item $profilePath -ItemType File
}

ise $profilePath

July 24, 2014 at 11:14 am

After taking your lead and some further research, it seems that powershell is getting these default values out of the registry, as I suspected:
HKEY_CURRENT_USER\Volatile Environment for User environment variables
HKEY_CURRENT_USER\Environment for System environment variables
HKEY_CURRENT_USER\Software\Microsoft\Windows\Explorer\User Shell Folders

and elsewhere!

Thanks Dave!