49

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

3
  • 1
    This was asked on Serverfault too serverfault.com/questions/195397/… Commented Feb 23, 2011 at 19:46
  • Thanks for the pointer . . . though in looking, I don't see a better solution there than is offered below. Though that posters reasons are a lot more compelling than mine . . . In this case, it isn't just a preference. Commented Feb 24, 2011 at 19:59
  • So you found a solution, awesome. What is $profile.AllUsersAllHosts then and how do you modify it?
    – flq
    Commented Aug 19, 2015 at 8:16

9 Answers 9

27

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.

6
  • Also a solution, but who wants to have to type all of that every time I invoke powershell. It isn't a good long term and/or permanent solution. Commented Feb 24, 2011 at 19:57
  • 5
    @Frank, that's why I suggested creating a shortcut that you can put on the desktop or start menu.
    – zdan
    Commented Feb 24, 2011 at 21:14
  • 5
    For reference a double ticky ('') is the escape sequence for a single ticky inside tickies. Took me a second to figure out what was going on. Commented Aug 4, 2013 at 16:35
  • 3
    the -file parameter will dot-source a file for you Commented Apr 29, 2015 at 9:11
  • The ISE apparently does not have the same parameters, you can only open files, skip the profile, and set multithreaded apartment mode.
    – Chris
    Commented Oct 17, 2019 at 15:02
22

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

4
  • I would recommend using the second part of this answer, especially if you do not care about the folder in mydocuments, and simply want to move your powershell profile for other reasons (I wanted to move my profile to another location so I could create a git repository for it). Commented May 28, 2018 at 0:29
  • 1
    IMO this is the best answer from a cost-effectiveness perspective. You can also use the same profile.ps1 file to modify the default PSModulePath installation path.
    – rsenna
    Commented Oct 8, 2019 at 8:05
  • @rsenna link is dead. This might be equivalent: stackoverflow.com/questions/56786702/…
    – YorSubs
    Commented Feb 13, 2020 at 23:22
  • Best solution for me. (both parts). Commented Feb 8, 2023 at 13:20
22

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.

3
  • 2
    the regedit option worked really well, i'm getting several errors on the PowerShell option, but it's fixed.
    – cajuuh
    Commented May 15, 2018 at 1:39
  • This is the only answer that works if the path is not present on the machine. You can check before and after values with $profile | select *
    – Marc
    Commented Mar 18, 2021 at 20:55
  • Thanks Nick. FYI, I think that just restarting explorer will be enough: Stop-Process -Name explorer -Force. Commented Aug 27 at 18:03
9

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.

1
  • 2
    This worked for me! I didn't even have to use a junction . A simple symbolic directory worked: mklink /D Commented Aug 4, 2013 at 21:53
9

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!

3
  • 1
    I tried this but it fails and says Cannot overwrite variable profile because it is read-only or constant.
    – Chris
    Commented Oct 17, 2019 at 15:35
  • Interesting... I managed to get this setup working on Powershell Core 7 today, as well as with Powershell 4 and 5 in the past. I suspect that perhaps that kind of an error message would arise if you didn't properly set up your execution policy, or if you ran the code I showed instead of pasting it. Or, perhaps there is some version of powershell or setup that invalidates my setup! I will work on revising my answer to make it more clear sometime. Commented May 16, 2020 at 17:08
  • 1
    I revisited this and it seems to be working now. I suppose it was likely an execution policy issue as you suggest, even though that is hard for me to believe. I routinely sign scripts and even past me should known how to avoid that issue. But perhaps not.
    – Chris
    Commented Jul 12, 2021 at 20:17
6

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

2
  • 1
    $profile.AllUsersAllHosts it is then. I'll try to make sure to do it in away that doesn't screw up other users, just in case somebody else happens to run PowerShell on my system. Commented Feb 24, 2011 at 20:01
  • 4
    I just can't believe MS hardcoded this location! How are we supposed to keep our development stuff separate from everything else?
    – ATL_DEV
    Commented Nov 4, 2012 at 0:09
3

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.

1
1

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.

1

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.