Frank Merrow
Frank Merrow

Reputation: 1009

Is it possible to change the default value of $profile to a new value?

So I would rather not create my profile file here:

C:\Users\fmerrow\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

I mean don't get me wrong, this isn't the end of the world and I can live with it. However, I like to keep root "My Documents" reasonably lean and I really would rather not create a directory there every time I start using a new application.

I've nosed around looking to where this setting might be hidden, but so far no luck. It doesn't seem to be in the registry or any of the $PsHome files.

Do I just have to learn to live with this? . . . or is there a way to change the value of $profile that will "stick" on this system for all time? That is, to change the "default value" of $profile?

The best I've thought of so far, is to ignore $profile and instead put some code in $profile.AllUsersAllHosts to source/execute my file from where I want to put it instead of from the default $profile location.

Comments and/or other suggestions welcomed.

Frank

Upvotes: 51

Views: 45097

Answers (9)

not2qubit
not2qubit

Reputation: 16930

The (user related) $PROFILE default folder is specified in the Personal registry item under:
HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\

The easiest way is to open powershell CLI.


# You can check the content of $PROFILE
$PROFILE | select *

AllUsersAllHosts       : C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost    : C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\xxxx\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\xxxx\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 67

# Check the Registry item:
$LP = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\"
Get-ItemPropertyValue -Path $LP -Name "Personal"

# C:\Users\wrong\Documents

# Set the new registry item to the right directory:
New-ItemProperty -Path $LP -Name 'Personal' -PropertyType String -Value "C:\Users\right\Documents" -Force | Out-Null

Restart Explorer.

References:

Upvotes: 1

Josh Desmond
Josh Desmond

Reputation: 690

This solution is inspired by RootLoop's answer:

Access your profile by navigating to its location defined by $PROFILE. (For me, that location happened to be C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1. Then, go ahead and move the contents of your customized profile to wherever you want it to be, (C:/NewLocation/profile.ps1, let's suppose). Replace the original profile's contents (the file C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1) with the text:

$profile = "C:\NewLocation\profile.ps1"
. $profile

Remember that the profile is just a script that is run as soon as you open powershell. This script above will first set $profile to the new location, so any references to the $profile variable will still work as if you moved it. The next line of code will invoke the new profile with syntax that is called dot sourcing. Effectively, the . $profile line is just running your new profile code.

Before that will work on your system, you may have to loosen your execution policy. See https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts for details on that.

Next, you can reduce the clutter in your My Documents directory by hiding the Powershell folder. Simply right click on the folder, select "properties", and under the general tab, select "hidden". And voila! - You have effectively created the illusion that you moved your profile location, without having to do much tinkering with system settings!

Upvotes: 9

Neck Beard
Neck Beard

Reputation: 433

You can change your $Profile.CurrentUser* paths by changing your personal folder path Environment.GetFolderPath(Environment.SpecialFolder.Personal)

Either via regedit

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

Under the Name column select Personal and chage the value to where you want your profile.

Or via PowerShell

New-ItemProperty 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' Personal -Value 'Your New Path Here' -Type ExpandString -Force

You have to reboot for this to take effect.

Upvotes: 24

Kevin Bridges
Kevin Bridges

Reputation: 186

This might be more of a workaround, but what I did was create a symbolic link copy of the WindowsPowerShell directory in the location PowerShell was looking at. This is more of a bandaid technique though.

Upvotes: 1

Yevgeniy
Yevgeniy

Reputation: 998

According to Scripting Guy article Understanding the Six PowerShell Profiles, $profile is expanded from $PsHome\Microsoft.PowerShell_profile.ps1; $pshome is the powershell installation directory and a read-only variable; according to a post on this thread, Microsoft tells us this cannot be changed.

Upvotes: 3

Root Loop
Root Loop

Reputation: 3162

You can also put your profile file here

C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

If you want to have a separated location for all your profiles and scripts, you can modify your profile.ps1 file above as

 $profile = "NewLocation\profile.ps1"
. $profile

Make sure what type of profile you use, see details here

https://technet.microsoft.com/en-ca/library/dd819434.aspx

Upvotes: 23

user1331496
user1331496

Reputation: 101

Try junctions by running this command in powershell:

cmd /c mklink /J c:\Users\Name\Documents\WindowsPowerShell\ d:\Powershell\Engine\Profile\

For more information about junctions see here.

Upvotes: 10

zdan
zdan

Reputation: 29450

The only thing I can think of is "dot sourcing" your profile at the powershell invocation.

For example:

powershell -noprofile -noexit -command "invoke-expression '. ''C:\My profile location\profile.ps1''' "

By changing the script that invoke-expression command points to you can place your "profile" anywhere you'd like. Then, create shortcut that launches PowerShell and set the target to the above command.

Upvotes: 27

Mike Shepard
Mike Shepard

Reputation: 18156

I think your solution to source your "new" profile in the existing profile is probably as good as you're going to get.

Upvotes: 6

Related Questions