형식 명령을 사용하여 출력 보기 변경

PowerShell에는 특정 개체에 대한 속성 표시 방법을 제어할 수 있는 일련의 cmdlet이 있습니다. 이러한 cmdlet의 이름은 모두 동사 Format으로 시작되며, 표시할 속성을 선택할 수 있습니다.

Get-Command -Verb Format -Module Microsoft.PowerShell.Utility
CommandType     Name               Version    Source
-----------     ----               -------    ------
Cmdlet          Format-Custom      6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Hex         6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-List        6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Table       6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Wide        6.1.0.0    Microsoft.PowerShell.Utility

이 문서에서는 Format-Wide, Format-ListFormat-Table cmdlet에 대해 설명합니다.

PowerShell의 각 개체 형식에는 표시할 속성을 선택하지 않는 경우에 사용되는 기본 속성이 있습니다. 각 cmdlet은 동일한 Property 매개 변수를 사용하여 표시할 속성을 지정합니다. 단일 속성만 표시하기 때문에 Format-Wide 속성 매개 변수는 단일 값만 사용하지만 속성 매개 변수는 Format-List 속성 이름 목록을 허용합니다Format-Table.

이 예에서 Get-Process cmdlet의 기본 출력은 두 개의 Internet Explorer 인스턴스가 실행 중임을 보여 줍니다.

Get-Process -Name iexplore

Process 개체의 기본 형식은 다음과 같은 속성을 표시합니다.

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     32    25.52      10.25      13.11   12808   1 iexplore
     52    11.46      26.46       3.55   21748   1 iexplore

Format-Wide를 사용하여 단일 항목 출력

기본적으로 cmdlet은 Format-Wide 개체의 기본 속성만 표시합니다. 각 개체와 연결된 정보는 단일 열에 표시됩니다.

Get-Command -Verb Format | Format-Wide
Format-Custom          Format-Hex
Format-List            Format-Table
Format-Wide

기본이 아닌 속성을 지정할 수도 있습니다.

Get-Command -Verb Format | Format-Wide -Property Noun
Custom                 Hex
List                   Table
Wide

열이 포함된 Format-Wide 표시 제어

cmdlet을 Format-Wide 사용하면 한 번에 하나의 속성만 표시할 수 있습니다. 이렇게 하면 여러 열에 큰 목록을 표시하는 데 유용합니다.

Get-Command -Verb Format | Format-Wide -Property Noun -Column 3
Custom                 Hex                  List
Table                  Wide

Format-List를 사용하여 목록 보기

cmdlet은 Format-List 각 속성에 레이블이 지정되고 별도의 줄에 표시되는 목록 형식으로 개체를 표시합니다.

Get-Process -Name iexplore | Format-List
Id      : 12808
Handles : 578
CPU     : 13.140625
SI      : 1
Name    : iexplore

Id      : 21748
Handles : 641
CPU     : 3.59375
SI      : 1
Name    : iexplore

다음과 같이 속성을 원하는 대로 지정할 수 있습니다.

Get-Process -Name iexplore | Format-List -Property ProcessName,FileVersion,StartTime,Id
ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime   : 10/22/2019 11:23:58 AM
Id          : 12808

ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime   : 10/22/2019 11:23:57 AM
Id          : 21748

와일드카드와 함께 Format-List를 사용하여 자세한 정보 가져오기

Format-List cmdlet을 사용하면 와일드카드를 해당 Property 매개 변수의 값으로 사용할 수 있습니다. 이렇게 하면 자세한 정보를 표시할 수 있습니다. 필요한 것보다 많은 정보가 개체에 포함되는 경우가 종종 있는데, 이는 PowerShell이 기본적으로 모든 속성 값을 표시하지는 않기 때문입니다. 개체의 모든 속성을 표시하려면 Format-List -Property * 명령을 사용합니다. 다음 명령은 단일 프로세스의 출력을 위해 60개 이상의 줄을 생성합니다.

Get-Process -Name iexplore | Format-List -Property *

Format-List 명령은 세부 정보를 표시하는 데 유용하지만 많은 항목이 포함된 출력 개요를 원하는 경우 더 간단한 테이블 형식 보기가 더 유용한 경우가 많습니다.

Format-Table을 사용하여 표 형식으로 출력

명령의 Format-Table 출력 Get-Process 형식을 지정하는 데 속성 이름이 지정되지 않은 cmdlet을 사용하는 경우 cmdlet 없이 수행하는 것과 정확히 동일한 출력을 Format 얻을 수 있습니다. 기본적으로 PowerShell은 프로세스 개체를 테이블 형식으로 표시합니다.

Get-Service -Name win* | Format-Table
Status   Name               DisplayName
------   ----               -----------
Running  WinDefend          Windows Defender Antivirus Service
Running  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Running  Winmgmt            Windows Management Instrumentation
Running  WinRM              Windows Remote Management (WS-Manag...

참고 항목

Get-Service는 Windows 플랫폼에서만 사용할 수 있습니다.

Format-Table 출력 향상

테이블 형식 보기는 많은 정보를 표시하는 데 유용하지만 표시가 데이터에 비해 너무 좁은지 해석하기 어려울 수 있습니다. 이전 예제에서는 출력이 잘립니다. Format-Table 명령을 실행할 때 AutoSize 매개 변수를 지정하면 PowerShell은 표시된 실제 데이터를 기준으로 열 너비를 계산합니다. 그러면 열을 읽을 수 있습니다.

Get-Service -Name win* | Format-Table -AutoSize
Status  Name                DisplayName
------  ----                -----------
Running WinDefend           Windows Defender Antivirus Service
Running WinHttpAutoProxySvc WinHTTP Web Proxy Auto-Discovery Service
Running Winmgmt             Windows Management Instrumentation
Running WinRM               Windows Remote Management (WS-Management)

cmdlet은 Format-Table 여전히 데이터를 잘리지만 화면 끝에만 잘립니다. 마지막으로 표시된 속성이 아닌 속성은 가장 긴 데이터 요소가 올바르게 표시되는 데 필요한 크기만큼 지정됩니다.

Get-Service -Name win* |
    Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -AutoSize
Name                 Status StartType DisplayName                               DependentServi
                                                                                ces
----                 ------ --------- -----------                               --------------
WinDefend           Running Automatic Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc, iphl…
Winmgmt             Running Automatic Windows Management Instrumentation        {vmms, TPHKLO…
WinRM               Running Automatic Windows Remote Management (WS-Management) {}

Format-Table 명령은 속성이 중요도 순서대로 나열된다고 가정합니다. cmdlet은 시작 부분에 가장 가까운 속성을 완전히 표시하려고 합니다. 명령에서 Format-Table 모든 속성을 표시할 수 없으면 표시에서 일부 열이 제거됩니다. DependentServices 속성의 이전 예에서 이 동작을 확인할 수 있습니다.

열에 Format-Table 출력 래핑

Wrap 매개 변수를 사용하여 긴 Format-Table 데이터를 해당 표시 열 내에서 래핑할 수 있습니다. 또한 AutoSize를 지정하지 않으면 기본 설정을 사용하므로 Wrap 매개 변수를 사용하면 예상한 것과 다를 수 있습니다.

Get-Service -Name win* |
    Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -Wrap
Name                 Status StartType DisplayName                               DependentServi
                                                                                ces
----                 ------ --------- -----------                               --------------
WinDefend           Running Automatic Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc,
                                                                                iphlpsvc}
Winmgmt             Running Automatic Windows Management Instrumentation        {vmms,
                                                                                TPHKLOAD,
                                                                                SUService,
                                                                                smstsmgr…}
WinRM               Running Automatic Windows Remote Management (WS-Management) {}

Wrap 매개 변수를 단독으로 사용하면 처리 속도가 크게 느려지지 않습니다. 그러나 AutoSize를 사용하여 큰 디렉터리 구조의 재귀 파일 목록의 형식을 지정하는 데 시간이 오래 걸리고 첫 번째 출력 항목을 표시하기 전에 많은 메모리를 사용할 수 있습니다.

시스템 로드에 대해 신경 쓰지 않는 경우 AutoSize는 Wrap 매개 변수와 잘 작동합니다. 초기 열은 여전히 한 줄에 항목을 표시하는 데 필요한 만큼 너비를 사용하지만 필요한 경우 최종 열은 래핑됩니다.

참고 항목

가장 넓은 열을 먼저 지정하면 일부 열이 표시되지 않을 수 있습니다. 최상의 결과를 위해서는 가장 작은 데이터 요소를 먼저 지정합니다.

다음 예에서는 너비가 가장 넓은 속성을 먼저 지정합니다.

Get-Process -Name iexplore |
    Format-Table -Wrap -AutoSize -Property FileVersion, Path, Name, Id

래핑이 있더라도 최종 ID 열은 생략됩니다.

FileVersion                          Path                                                  Nam
                                                                                           e
-----------                          ----                                                  ---
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE iex
                                                                                           plo
                                                                                           re
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files\Internet Explorer\iexplore.exe       iex
                                                                                           plo
                                                                                           re

테이블 출력 구성(-GroupBy)

테이블 형식 출력 컨트롤에 대한 또 다른 유용한 매개 변수는 GroupBy입니다. 특히 긴 표 형식의 목록은 비교하기 어려울 수 있지만 GroupBy 매개 변수는 속성 값에 따라 출력을 그룹화합니다. 예를 들어 더 쉽게 검사할 수 있도록 StartType별로 서비스를 그룹화하고 속성 목록에서 StartType 값을 생략할 수 있습니다.

Get-Service -Name win* | Sort-Object StartType | Format-Table -GroupBy StartType
   StartType: Automatic
Status   Name               DisplayName
------   ----               -----------
Running  WinDefend          Windows Defender Antivirus Service
Running  Winmgmt            Windows Management Instrumentation
Running  WinRM              Windows Remote Management (WS-Managem…

   StartType: Manual
Status   Name               DisplayName
------   ----               -----------
Running  WinHttpAutoProxyS… WinHTTP Web Proxy Auto-Discovery Serv…