Microsoft PowerShell은 명령줄 셸, 스크립팅 언어 및 구성 관리 프레임워크로 구성된 플랫폼 간 작업 자동화 솔루션이다.
이러한 이유로 Windows에도 PowerShell이 기본 cli 도구로 탑재되어 있다.
PowerShell이 주로 고급 사용자에게 사용되는 만큼, PowerShell은 현재 세션에서 실행한 모든 명령의 기록을 유지하고, 이를 조회하거나 검색할 수 있는 여러 cmdlet을 제공하는 기능이 있다.
이 기능 덕분에, PowerShell을 킨 후, 위아래 방향키를 눌러 이전에 입력한 cmdlet을 불러올 수 있다.
PowerShell 명령 기록은 PSReadline 모듈에 의해 텍스트 파일로 기록되며, 이 글에서는 해당 기록의 위치 및 해당 기록을 열람하는 방법, 기록 기능에 대한 옵션 설정을 안내할 것이다.
여담으로 Cmdlets란 PowerShell 명령으로, Command-lets으로 발음한다.
파일의 경로
명령 기록을 열람하는 방법 중 가장 확실한 방법은 파일 탐색기를 통해 파일 경로로 들어가 파일을 직접 여는 것이다.
파일의 경로는 아래와 같다.
C:\Users\<사용자 이름>\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
%Userprofile%\AppData\Roaming\Microsoft\Windows\Powershell\PSReadLine\ConsoleHost_history.txt
%Userprofile% 환경 변수는 사용자 폴더를 가리키는 변수이다.
%Appdata%\Microsoft\Windows\Powershell\PSReadLine\ConsoleHost_history.txt
%Appdata% 환경 변수는 "%Userprofile%\AppData\Roaming"을 가리키는 변수이다.
세 경로 모두 같은 경로를 가리키므로, 파일 탐색기를 통해 해당 경로로 직접 들어가 ConsoleHost_history.txt를 직접 열거나, 해당 경로를 명령 프롬프트에 입력하여 파일을 열 수 있다.
여담으로 해당 경로를 PowerShell에 입력하면 인식하지 못한다.
위와 같이 오류가 뜬다.
기록 설정
PowerShell에 "Get-PSReadlineOption" cmdlet을 입력하면 사용 가능한 PSReadLine 옵션 목록과 현재 값을 반환한다.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
새로운 기능 및 개선 사항에 대 한 최신 PowerShell을 설치 하세요! https://aka.ms/PSWindows
PS C:\Windows\system32> Get-PSReadlineOption
EditMode : Windows
AddToHistoryHandler : System.Func`2[System.String,System.Object]
HistoryNoDuplicates : True
HistorySavePath : C:\Users\<username>\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\Console
Host_history.txt
HistorySaveStyle : SaveIncrementally
HistorySearchCaseSensitive : False
HistorySearchCursorMovesToEnd : False
MaximumHistoryCount : 4096
ContinuationPrompt : >>
ExtraPromptLineCount : 0
PromptText : {> }
BellStyle : Audible
DingDuration : 50
DingTone : 1221
CommandsToValidateScriptBlockArguments : {ForEach-Object, %, Invoke-Command, icm...}
CommandValidationHandler :
CompletionQueryItems : 100
MaximumKillRingCount : 10
ShowToolTips : True
ViModeIndicator : None
WordDelimiters : ;:,.[]{}()/\|^&*-=+'"–—―
AnsiEscapeTimeout : 100
CommandColor : "$([char]0x1b)[93m"
CommentColor : "$([char]0x1b)[32m"
ContinuationPromptColor : "$([char]0x1b)[37m"
DefaultTokenColor : "$([char]0x1b)[37m"
EmphasisColor : "$([char]0x1b)[96m"
ErrorColor : "$([char]0x1b)[91m"
KeywordColor : "$([char]0x1b)[92m"
MemberColor : "$([char]0x1b)[97m"
NumberColor : "$([char]0x1b)[97m"
OperatorColor : "$([char]0x1b)[90m"
ParameterColor : "$([char]0x1b)[90m"
SelectionColor : "$([char]0x1b)[30;47m"
StringColor : "$([char]0x1b)[36m"
TypeColor : "$([char]0x1b)[37m"
VariableColor : "$([char]0x1b)[92m"
이 중 만져볼 만한 것은 -HistorySavePath, -HistorySaveStyle, -MaximumHistoryCount 정도이다.
-HistorySavePath
기록이 저장되는 파일의 경로를 지정합니다. Windows 또는 비 Windows 플랫폼을 실행하는 컴퓨터는 파일을 다른 위치에 저장합니다. 파일 이름은 변수 $($Host.Name)_history.txt(예 ConsoleHost_history.txt: )에 저장됩니다.
이 매개 변수를 사용하지 않는 경우 기본 경로는 다음과 같습니다.
Windows
$env:%APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\$($Host.Name)_history.txt
비 Windows
$env:XDG_DATA_HOME/powershell/PSReadLine/$($Host.Name)_history.txt
$HOME/.local/share/powershell/PSReadLine/$($Host.Name)_history.txt
-HistorySaveStyle
PSReadLine에서 기록을 저장하는 방법을 지정합니다.
유효한 값은 다음과 같습니다.
SaveIncrementally: 각 명령이 실행된 후 기록을 저장하고 PowerShell의 여러 인스턴스에서 공유합니다.
SaveAtExit: PowerShell이 종료되면 기록 파일을 추가합니다.
SaveNothing: 기록 파일을 사용하지 마세요.
-MaximumHistoryCount
PSReadLine 기록에 저장할 최대 명령 수를 지정합니다.
PSReadLine 기록은 PowerShell 기록과 별개입니다.
이들을 이용하여 기록이 저장되는 방법을 지정할 수 있다.
기록 파일의 경로와 이름을 지정하는 -HistorySavePath 파라미터는 다음과 같이 사용할 수 있다.
Set-PSReadlineOption -HistorySavePath "<원하는 경로>\<원하는 이름>_history.txt"
가령 "D\History"디렉터리에 "PowerShell_history.txt"라는 이름으로 기록을 저장하고 싶다면
Set-PSReadlineOption -HistorySavePath "D:\History\PowerShell_history.txt"
라고 입력하면 된다.
PSReadLine에서 기록을 저장하는 방법을 지정하는 -HistorySaveStyle은 다음과 같이 이용할 수 있다.
Cmdlet이 실행된 후 기록을 저장하고 싶을 때
Set-PSReadlineOption -HistorySaveStyle
PowerShell이 종료된 후 기록을 저장하고 싶을 때
Set-PSReadlineOption -HistorySaveStyle SaveAtExit
기록 파일을 사용하고 싶지 않을 때
Set-PSReadlineOption -HistorySaveStyle SaveNothing
PSReadLine 기록에 저장할 최대 명령 수를 지정하는 방법은 다음과 같다.
Set-PSReadlineOption -MaximumHistoryCount <개수>
예를 들어 저장할 명령 개수를 40개로 제한하고 싶다면
Set-PSReadlineOption -MaximumHistoryCount 40
이라고 입력하면 된다.
여담
ConsoleHost_history.txt의 내용은 사용자가 수정할 수 있다.
해당 파일이 그저 기록 파일이기 때문인데, 이 때문에 잘못 입력한 명령어까지 저장된다.
따라서 해당 파일에서 잘못 입력한 명령어나, 딱히 기록해두고 싶지 않은 명령어를 제거하는 것도 좋다. 상술하였듯이 해당 기록은 위, 아래 방향키로 불러올 수 있기 때문이다.
참고 자료
Get-PSReadLineOption (PSReadLine) - PowerShell | Microsoft Learn
Get-PSReadLineOption (PSReadLine) - PowerShell
cmdlet은 Get-PSReadLineOption cmdlet을 사용하여 Set-PSReadLineOption 구성할 수 있는 설정의 현재 상태를 반환합니다. 반환된 개체를 사용하여 PSReadLine 옵션을 변경할 수 있습니다. 이렇게 하면 여러 종류의
learn.microsoft.com
Set-PSReadLineOption (PSReadLine) - PowerShell | Microsoft Learn
Set-PSReadLineOption (PSReadLine) - PowerShell
cmdlet은 Set-PSReadLineOption 명령줄을 편집할 때 PSReadLine 모듈의 동작을 사용자 지정합니다. PSReadLine 설정을 보려면 를 사용합니다Get-PSReadLineOption. 이 명령에서 설정한 옵션은 현재 세션에만 적용됩
learn.microsoft.com
'컴퓨터 > Windows' 카테고리의 다른 글
DISM을 이용하여 오프라인 Windows에 업데이트 패키지 추가 (0) | 2023.09.04 |
---|---|
Windows에서 S1-S3 절전 모드 설정 (0) | 2023.08.29 |
유효성 검사 OS를 이용한 Windows 무결성 검사 (0) | 2023.08.21 |
고급 사용자 계정 제어판(Netplwiz)을 이용한 계정 설정 (0) | 2023.08.21 |
오프라인 디렉터리에서의 Windows 무결성 검사 (0) | 2023.08.18 |
댓글