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

前端 未结 8 1516
予麋鹿
予麋鹿 2020-12-10 01:18

So I would rather not create my profile file here:

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

I mea

相关标签:
8条回答
  • 2020-12-10 01:37

    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.

    0 讨论(0)
  • 2020-12-10 01:39

    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.

    0 讨论(0)
  • 2020-12-10 01:39

    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!

    0 讨论(0)
  • 2020-12-10 01:40

    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.

    0 讨论(0)
  • 2020-12-10 01:43

    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.

    0 讨论(0)
  • 2020-12-10 01:45

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

    0 讨论(0)
提交回复
热议问题