How do I configure a startup/config script for Powershell?

♀尐吖头ヾ 提交于 2021-01-04 14:54:05

问题


I am looking to define a set of custom aliases/variables for use every time I launch PowerShell. I am looking for the PowerShell alternative to the .bashrc file.

I have tried putting a file named Profile.ps1 in the home directory, but it never runs. What do I need to do to get this file to run?

I am using Windows 10.


回答1:


I'd start by reading through Microsoft's documentation about_Profiles. I've linked to the 5.1 version of the documentation since that's likely what you have on Windows 10, though I doubt it has changed much from version to version. It includes instructions on how to actually create the file and details about when it runs.

For your use case, you probably want to do the following from your session:

if (!(Test-Path -Path $PROFILE)) {
  New-Item -ItemType File -Path $PROFILE -Force
}

which will generate the profile. Then once that is generated you can open it with notepad directly with the same example from there:

notepad $PROFILE

You can substitute your favorite editor for notepad. VSCode for instance would be:

code $PROFILE

You can also just get the path to the your profile by simply typing $PROFILE into the shell and it will output it, and then use that information to navigate to it from your favorite editor.

Per the documentation:

A PowerShell profile is a script that runs when PowerShell starts.

It's more complicated than that since there are multiple profile files as the documentation explains, but for your story it's accurate enough.

You can always also 'dotsource' your profile to make it run manually by executing:

. $Profile

However you should note that objects previously defined by prior invocations of your profile might not be removed. So while developing your profile that's a useful technique, be sure to only trust how it behaves when run by opening a completely new session.




回答2:


I have tried putting a file named Profile.ps1 in the home directory, but it never runs.

Profile.ps1 will work, though not in the $Home directory, but rather in the $Home\[My ]Documents\[Windows]PowerShell\Profile.ps1. The actual combination between [My ]Documents and [Windows]PowerShell depends on the versions of Windows and PowerShell, as documented here.

On my Windows 10 with PS 5.1 the right place for profile.ps1 is $home\Documents\WindowsPowerShell.

The candidate locations for profile files can also be checked via PowerShell itself.

PS C:\etc> $profile | get-member -name *user*

   TypeName: System.String

Name                   MemberType   Definition
----                   ----------   ----------
AllUsersAllHosts       NoteProperty string AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    NoteProperty string AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    NoteProperty string CurrentUserAllHosts=C:\Users\XXXXXXXX\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost NoteProperty string CurrentUserCurrentHost=C:\Users\XXXXXXXX\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1


来源:https://stackoverflow.com/questions/61492112/how-do-i-configure-a-startup-config-script-for-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!