PowerShell 프로필은 PowerShell이 시작될 때 실행되는 스크립트입니다. 프로필을 사용하여 환경을 사용자 지정할 수 있습니다. 당신은 할 수 있어요:
- 별칭, 함수 및 변수 추가
- 모듈 로드
- PowerShell 드라이브 만들기
- 임의의 명령 실행
- 기본 설정 변경
이러한 설정을 프로필에 배치하면 시스템에서 PowerShell을 시작할 때마다 사용할 수 있습니다.
비고
Windows에서 스크립트를 실행하려면 PowerShell 실행 정책을 최소한 RemoteSigned 설정해야 합니다. 실행 정책은 macOS 및 Linux에 적용되지 않습니다. 자세한 내용은 about_Execution_Policy참조하시기 바랍니다.
$PROFILE 변수
$PROFILE 자동 변수는 현재 세션에서 사용할 수 있는 PowerShell 프로필의 경로를 저장합니다.
다양한 사용자 범위 및 다른 PowerShell 호스트를 지원하는 데 사용할 수 있는 네 가지 가능한 프로필이 있습니다. 각 프로필 스크립트에 대한 정규화된 경로는 $PROFILE다음 멤버 속성에 저장됩니다.
- AllUsersAllHosts
- 모든사용자현재호스트
- CurrentUserAllHosts
- 사용중인사용자사용중인호스트
모든 사용자 또는 특정 사용자에 대해 실행되는 프로필 스크립트를 만들 수 있습니다. 특정 사용자는 CurrentUser입니다. CurrentUser 프로필은 사용자의 홈 디렉터리 경로 아래에 저장됩니다. 위치는 사용하는 운영 체제 및 PowerShell 버전에 따라 달라집니다.
기본적으로 $PROFILE 변수를 참조하면 "현재 사용자, 현재 호스트" 프로필의 경로가 반환됩니다. 다른 프로필 경로는 $PROFILE 변수의 속성을 통해 액세스할 수 있습니다.
다음 명령은 Windows의 기본 프로필 위치를 보여 줍니다.
PS> $PROFILE | Select-Object *
AllUsersAllHosts : C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost : C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\username\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\username\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Length : 69
다음 명령은 Ubuntu Linux의 기본 프로필 위치를 보여줍니다.
$PROFILE | Select-Object *
AllUsersAllHosts : /opt/microsoft/powershell/7/profile.ps1
AllUsersCurrentHost : /opt/microsoft/powershell/7/Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : /home/username/.config/powershell/profile.ps1
CurrentUserCurrentHost : /home/username/.config/powershell/Microsoft.PowerShell_profile.ps1
Length : 67
모든 PowerShell 호스트 또는 특정 호스트에 대해 실행되는 프로필도 있습니다. 각 PowerShell 호스트의 프로필 스크립트에는 해당 호스트에 대한 고유한 이름이 있습니다. 예를 들어 Windows의 표준 콘솔 호스트 또는 다른 플랫폼의 기본 터미널 애플리케이션에 대한 파일 이름은 Microsoft.PowerShell_profile.ps1. VS Code(Visual Studio Code)의 경우 파일 이름은 Microsoft.VSCode_profile.ps1.
자세한 내용은 about_Profiles참조하세요.
개인 프로필을 만드는 방법
시스템에 PowerShell을 처음 설치할 때 프로필 스크립트 파일과 해당 파일이 속한 디렉터리도 존재하지 않습니다. 다음 명령은 "현재 사용자, 현재 호스트" 프로필 스크립트 파일이 없는 경우 만듭니다.
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
cmdlet의 New-Item 매개 변수가 필요한 폴더가 없는 경우 이를 생성합니다.
스크립트 파일을 만든 후 즐겨 찾는 편집기를 사용하여 셸 환경을 사용자 지정할 수 있습니다.
프로필에 사용자 지정 추가
이전 문서에서는 탭 완성, 명령 예측자및 별칭사용하는 방법에 대해 설명했습니다. 이 문서에서는 필요한 모듈을 로드하고, 사용자 지정 완료자를 만들고, 키 바인딩 및 기타 설정을 정의하는 데 사용되는 명령을 보여 줍니다. 이러한 사용자 지정은 모든 PowerShell 대화형 세션에서 사용할 수 있도록 하려는 종류입니다. 프로필 스크립트는 이러한 설정의 위치입니다.
프로필 스크립트를 편집하는 가장 간단한 방법은 즐겨 찾는 코드 편집기에서 파일을 여는 것입니다. 예를 들어 다음 명령은 VS Code프로필을 엽니다.
code $PROFILE
Windows, Linux의 notepad.exe 또는 다른 텍스트 편집기에서 vi 사용할 수도 있습니다.
다음 프로필 스크립트에는 이전 문서에서 언급한 많은 사용자 지정에 대한 예제가 있습니다. 사용자 고유의 프로필에서 이러한 설정을 사용할 수 있습니다.
## Map PSDrives to other registry hives
if (!(Test-Path HKCR:)) {
$null = New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
$null = New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS
}
## Customize the prompt
function prompt {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
$prefix = if (Test-Path Variable:/PSDebugContext) { '[DBG]: ' } else { '' }
if ($principal.IsInRole($adminRole)) {
$prefix = "[ADMIN]:$prefix"
}
$body = 'PS ' + $PWD.path
$suffix = $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
"${prefix}${body}${suffix}"
}
## Create $PSStyle if running on a version older than 7.2
## - Add other ANSI color definitions as needed
if ($PSVersionTable.PSVersion.ToString() -lt '7.2.0') {
# define escape char since "`e" may not be supported
$esc = [char]0x1b
$PSStyle = [pscustomobject]@{
Foreground = @{
Magenta = "${esc}[35m"
BrightYellow = "${esc}[93m"
}
Background = @{
BrightBlack = "${esc}[100m"
}
}
}
## Set PSReadLine options and keybindings
$PSROptions = @{
ContinuationPrompt = ' '
Colors = @{
Operator = $PSStyle.Foreground.Magenta
Parameter = $PSStyle.Foreground.Magenta
Selection = $PSStyle.Background.BrightBlack
InLinePrediction = $PSStyle.Foreground.BrightYellow + $PSStyle.Background.BrightBlack
}
}
Set-PSReadLineOption @PSROptions
Set-PSReadLineKeyHandler -Chord 'Ctrl+f' -Function ForwardWord
Set-PSReadLineKeyHandler -Chord 'Enter' -Function ValidateAndAcceptLine
## Add argument completer for the dotnet CLI tool
$scriptblock = {
param($wordToComplete, $commandAst, $cursorPosition)
dotnet complete --position $cursorPosition $commandAst.ToString() |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock $scriptblock
이 프로필 스크립트는 다음 사용자 지정에 대한 예제를 제공합니다.
- 다른 루트 레지스트리 하이브용 두 개의 새 PSDrive를 추가합니다.
- 권한 상승 세션에서 실행되는 경우 변경되는 사용자 지정 프롬프트을 만듭니다.
- PSReadLine 구성하고 키 바인딩을 추가합니다. 색 설정은 $PSStyle 기능을 사용하여 ANSI 색 설정을 정의합니다.
- dotnet CLI 도구에 대한 탭 완성을 추가합니다. 이 도구는 명령줄 인수를 해결하는 데 도움이 되는 매개 변수를 제공합니다. Register-ArgumentCompleter 대한 스크립트 블록은 해당 기능을 사용하여 탭 완성을 제공합니다.
PowerShell