계기
- Powershell 이 실행될 때, 자동으로 실행되는 코드를 담은 파일이 있고, 이 파일의 내용이 자동으로 실행됩니다.
- 이 파일은 profile 파일이라고 부르는데, powershell 환경에서 $profile 이라는 변수가 이 내용을 가지고 있습니다.
- 예전의 MS-DOS의 자동실행 파일인 autoexec.bat 와 같은 역할을 합니다.
- python에서는
PYTHONSTARTUP
라는 변수의 path의 파일을 읽으려고 합니다.- ~/.bash_profile 에 보통 적으두죠
- export PYTHONSTARTUP=~/.pythonstartup
- 윈도우즈는 setx 를 사용해서 변수의 값을 정의합니다.
- C:> setx PYTHONSTARTUP D:/x/myconf/pythonstartup.py
- ipython 이라면 ~/.ipython/profile_default/ 에 있는 .py 파일이나 .ipy 파일들을 사용합니다.
- ~/.bash_profile 에 보통 적으두죠
- 보통은
notepad $profile
과 같이 사용합니다. - 이 파일의 동작에 대해 알아보겠습니다.
조사
- 우선 이 profile을 읽지않고 powershell을 실행하는 방법은
PowerShell –noprofile
과 같이 실행하는 방법이 있습니다. - 이상한 것은 $profile 의 멤버에는
NoteProperty
라는 이상한 property가 있는데, 조금 특별한 값을 가지고 있습니다.- 특별변수, 예를 들면 $pshome 과 같은 변수에는
NoteProperty
라는 property가 없습니다. - 이러한 값을 가지고 있습니다.
(base) PS C:\Users\Administrator> $profile | gm -membertype noteproperty 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\Administrator\Documents\WindowsPowerShell\profile.ps1 CurrentUserCurrentHost NoteProperty string CurrentUserCurrentHost=C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
- 즉 4개의 다른 ps1 파일의 값을 가지고 있습니다.
- $profile 에 나오는 값은 제일 긴 이름의
CurrentUserCurrentHost
의 값이 나오고 있었습니다.
- 특별변수, 예를 들면 $pshome 과 같은 변수에는
실험
- powershell ise에서는 어떻게 되어있을까요?
AllUsersAllHosts NoteProperty string AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 AllUsersCurrentHost NoteProperty string AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1 CurrentUserAllHosts NoteProperty string CurrentUserAllHosts=C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1 CurrentUserCurrentHost NoteProperty string CurrentUserCurrentHost=C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
- powershell 이라는 단어대신 PowerShellISE 라는 단어로 대체되어 있었습니다.
- 물론 그냥 $profile의 값을 불러오면 역시 제일 긴 이름의
CurrentUserCurrentHost
의 값이 나오고 있었습니다.
- 물론 그냥 $profile의 값을 불러오면 역시 제일 긴 이름의
응용
- vscode 의 경우는 어떨까요
AllUsersCurrentHost 와 CurrentUserCurrentHost 는 powershell 단어가 아닌 vscode 단어를 사용한 값이 나옵니다.- $profile의 값을 불러오면 역시 제일 긴 이름의
CurrentUserCurrentHost
의 값이 나오고 있었습니다.- powershell host를 제작할 때는 이렇게 해야하는 규칙이 있는 건 아닐까요?
- vscode는
psedit
이라는 명령을 내장하고 있기 때문에psedit $profile
과 같은 형태로 사용합니다.
- $profile의 값을 불러오면 역시 제일 긴 이름의
- remote powershell로 접속하면 이 변수는 존재할가요?
- 아니요 존재하지 않습니다.
결과
- $profile을 어떻게 사용하면 좋을까요?
- 개인적인 github key나 api secret등을 변수를 정의해 두기도 합니다.
- 저는 간혹 자주 연결하는 호스트의 세션을 만들어두는 함수를 정의해 두기도 합니다.
- 간혹 starship에 해당하는 명령을 넣어두기도 합니다.
# start starship Invoke-Expression (&starship init powershell) $starshipPrompt = (Get-Item Function:\prompt).ScriptBlock
- 이상으로 $profile의 사용방법과 예를 알아보았습니다.