Powershell是否有任何等效的.bashrc?


Answers:


23

jehad的注释中提到的文章解释说,PowerShell可在多个位置加载配置文件,这正是您想要的。您可能希望每用户一个普通的PowerShell控制台。$profile变量中提供了PowerShell将检查该文件的路径。您可以使用以下命令创建该文件及其包含的目录:

New-Item $profile -Type File -Force

它在Documents文件Microsoft.PowerShell_profile.ps1WindowsPowerShell下的文件夹中创建一个文件。然后,您可以使用文本编辑器将其打开:

notepad $profile

每当您启动PowerShell控制台时,无论您是否处于提升状态,其中的所有内容都将运行。我用另一篇文章来生成一个函数(可以用作cmdlet)来检查当前PowerShell实例是否升高。将其放入新的配置文件中:

Function Test-Elevated {
  $wid = [System.Security.Principal.WindowsIdentity]::GetCurrent()
  $prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
  $adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
  $prp.IsInRole($adm)
}

该函数可以在您正常的PowerShell体验中使用,但是您也可以使用它仅在提升权限运行时运行配置文件中的内容:

If (Test-Elevated) {
  echo "Be careful!"
} Else {
  echo "Eh, do whatever."
}

由于此文件包含即使在管理PowerShell实例下也将自动运行的代码,因此您不希望运行未提升的程序对其具有写权限。我建议更改其ACL,使其仅授予您的用户帐户读取权限,同时仍允许管理员完全控制。(必须先禁用继承。)然后,您只能从高级程序中编辑脚本。


3
可以将这个长名称Microsoft.PowerShell_profile.ps1更改为just profile.ps1吗?在第一篇文章中,它看起来是$Home\[My ]Documents\WindowsPowerShell\Profile.ps1
nephewtom
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.