파일 시스템에 대한 Get-Content

업데이트 날짜: 2015년 8월

적용 대상: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

파일의 내용을 가져옵니다.

구문

Get-Content [-Delimiter <string>] [-Encoding {<Unknown> | <String> | <Unicode> | <Byte> | <BigEndianUnicode> | <UTF8> | <UTF7> | <UTF32> | <Ascii> | <Default> | <Oem>}] [-Force] [-Raw <switch>] [-Stream <string>] [-Wait] [-UseTransaction] [<CommonParameters>]

설명

파일 시스템 드라이브에서 Get-Content cmdlet을 사용하여 명령줄에 표시하거나, 처리를 위해 변수에 저장하거나, 다른 파일에 쓰는 내용을 가져올 수 있습니다. 폴더에는 이 명령을 사용할 수 없습니다.

참고: 이 사용자 지정 cmdlet 도움말 파일은 파일 시스템 드라이브에서 Get-Content cmdlet이 작동하는 방식을 설명합니다. 모든 드라이브의 Get-Content cmdlet에 대한 자세한 내용은 "Get-HelpGet-Content -Path $null"을 입력하거나 Get-Content(https://go.microsoft.com/fwlink/?LinkID=113310)를 참조하세요.

매개 변수

-Encoding <FileSystemCmdletProviderEncoding>

파일 인코딩을 지정합니다. 기본값은 ASCII입니다.

유효한 값은

-- ASCII: ASCII(7비트) 문자 집합에 대한 인코딩을 사용합니다.

-- BigEndianUnicode: Big-Endian 바이트 순서를 사용하여 UTF-16 형식으로 인코딩합니다.

-- Byte: 문자 집합을 일련의 바이트로 인코딩합니다.

-- String: 문자열에 대한 인코딩 유형을 사용합니다.

-- Unicode: Little-Endian 바이트 순서를 사용하여 UTF-16 형식으로 인코딩합니다.

-- UTF7: UTF-7 형식으로 인코딩합니다.

-- UTF8: UTF-8 형식으로 인코딩합니다.

-- Unknown: 인코딩 유형을 알 수 없거나 잘못되었습니다. 데이터가 Binary로 처리될 수 있습니다.

Encoding은 파일 시스템 공급자가 Get-Content cmdlet에 추가하는 동적 매개 변수입니다. 이 매개 변수는 파일 시스템 드라이브에만 작동합니다.

이진 파일을 읽고 이진 파일에 쓸 때 Encoding 동적 매개 변수에 대해 Byte 값을 사용하고 ReadCount 매개 변수에 대해 0 값을 사용합니다. ReadCount 값이 0인 경우 단일 읽기 작업으로 전체 파일을 읽고 단일 개체(PSObject)로 변환합니다. 기본 ReadCount 값 1은 각 읽기 작업에서 1바이트를 읽고 각 바이트를 별도의 개체로 변환합니다. 따라서 Set-Content cmdlet을 사용하여 파일에 바이트를 쓸 때는 오류가 발생합니다. 자세한 내용은 예제를 참조하세요.

필수 여부

false

위치

명명됨

기본값

ASCII

파이프라인 입력 적용 여부

false

와일드카드 문자 허용 여부

false

-Delimiter <string>

Get-Content가 파일을 읽는 동안 파일을 개체로 나누는 데 사용하는 구분 기호를 지정합니다.

기본값은 줄 끝 문자에 해당하는 "\n"입니다.

따라서 기본적으로 텍스트 파일을 읽는 동안 Get-Content는 각각이 줄 끝 문자로 끝나는 문자열 개체의 컬렉션을 반환합니다.

파일에서 존재하지 않는 구분 기호를 입력하면 Get-Content는 전체 파일을 구분되지 않은 단일 개체로 반환합니다.

이 매개 변수를 사용하여 "End of Example"과 같은 파일 구분 기호를 지정하여 큰 파일을 여러 개의 더 작은 파일로 분할할 수 있습니다. 구분 기호는 보존되고(삭제되지 않음) 각 파일 섹션의 마지막 항목이 됩니다.

Delimiter는 파일 시스템 공급자가 Get-Content cmdlet에 추가하는 동적 매개 변수입니다. 이 매개 변수는 파일 시스템 드라이브에만 작동합니다.

문제 해결 참고: 현재 Delimiter 매개 변수의 값이 빈 문자열이면 Get-Content는 아무것도 반환하지 않습니다. 이것은 알려진 문제입니다. 강제로 Get-Content가 전체 파일을 구분되지 않은 단일 문자열로 반환하게 하려면 파일에 존재하지 않는 값을 입력합니다.

필수 여부

false

위치

명명됨

기본값

줄 끝 문자

파이프라인 입력 적용 여부

false

와일드카드 문자 허용 여부

false

-Force

숨겨진 파일을 포함하여 모든 파일의 내용을 가져옵니다. 기본적으로Get-Content는 이름으로 숨겨진 파일을 지정하지 않으면 숨겨진 파일의 내용을 가져오지 않습니다.

필수 여부

false

위치

명명됨

기본값

False

파이프라인 입력 적용 여부

false

와일드카드 문자 허용 여부

false

-Raw <switch>

줄 바꿈 문자를 무시하고 파일의 전체 내용을 하나의 문자열로 반환합니다. 기본적으로 파일의 내용이 줄 바꿈 문자로 구분되는 문자열의 배열로 반환됩니다.

Raw는 파일 시스템 공급자가 Get-Content cmdlet에 추가하는 동적 매개 변수입니다. 이 매개 변수는 파일 시스템 드라이브에만 작동합니다.

이 매개 변수는 Windows PowerShell 3.0에서 도입되었습니다.

필수 여부

false

위치

명명됨

기본값

파이프라인 입력 적용 여부

false

와일드카드 문자 허용 여부

false

-Wait

명령 프롬프트를 반환하기 전에 cmdlet이 내용을 가져올 때까지 기다립니다. 기다리는 동안 Get-Content는 Ctrl+C를 눌러 사용자가 중단할 때까지 1초에 한 번씩 파일을 검사합니다.

Wait는 파일 시스템 공급자가 Get-Content cmdlet에 추가하는 동적 매개 변수입니다. 이 매개 변수는 파일 시스템 드라이브에만 작동합니다.

필수 여부

false

위치

명명됨

기본값

False

파이프라인 입력 적용 여부

false

와일드카드 문자 허용 여부

false

-Stream <string>

파일에서 지정된 대체 NTFS 파일 스트림의 내용을 가져옵니다. 스트림 이름을 입력합니다. 와일드카드는 지원되지 않습니다.

Stream은 파일 시스템 공급자는 Get-Content cmdlet을 추가하는 동적 매개 변수입니다. 이 매개 변수는 파일 시스템 드라이브에만 작동합니다.

이 매개 변수는 Windows PowerShell 3.0에서 도입되었습니다.

필수 여부

false

위치

명명됨

기본값

파이프라인 입력 적용 여부

false

와일드카드 문자 허용 여부

false

-UseTransaction

활성 트랜잭션에 명령을 포함합니다. 이 매개 변수는 트랜잭션이 진행 중일 경우에만 유효합니다. 자세한 내용은 about_Transactions를 참조하세요.

필수 여부

false

위치

명명됨

기본값

파이프라인 입력 적용 여부

false

와일드카드 문자 허용 여부

false

<CommonParameters>

이 cmdlet은 일반 매개 변수 -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, -OutVariable, -Verbose, -WarningAction 및 -WarningVariable을 지원합니다. 자세한 내용은 about_CommonParameters를 참조하세요.

입력 및 출력

입력 유형은 cmdlet에 파이프할 수 있는 개체의 유형입니다. 반환 유형은 cmdlet에서 반환되는 개체의 유형입니다.

입력

System.Int64, System.String[], System.Management.Automation.PSCredential

읽기 수, 총 개수, 경로 또는 자격 증명을 Get-Content로 파이프할 수 있습니다.

출력

System.Object, System.String

Get-Content는 가져오는 내용을 나타내는 개체가 반환합니다. 개체 형식은 내용 형식에 따라 달라집니다. Stream 매개 변수를 사용하는 경우 cmdlet은 대체 데이터 스트림 내용을 문자열로 반환합니다.

예제 1

C:\PS>Get-Content -Path C:\Chapters\chapter1.txt

Description
-----------
This command gets the content of the Chapter1.txt file and displays it in the console. It uses the Path parameter to specify the name of the item. 

Get-Content actually passes the content down the pipeline, but because there are no other cmdlets in the pipeline, Windows PowerShell formats the contents and displays it in the console.





예제 2

C:\PS>Get-Content C:\Logs\Log060912.txt -TotalCount 50 | Set-Content Sample.txt

Description
-----------
This command gets the first 50 lines of the Log060912.txt file and stores them in the sample.txt file. 

The command uses the Get-Content cmdlet to get the text in the file. (The name of Path parameter, which is optional, is omitted.) The TotalCount parameter limits the retrieval to the first 50 lines. The pipeline operator (|) sends the result to Set-Content, which places it in the sample.txt file.





예제 3

C:\PS>(Get-Content Cmdlets.txt -TotalCount 5)[-1]

Description
-----------
This command gets the fifth line of the Cmdlets.txt text file. It uses the TotalCount parameter to get the first five lines and then uses array notation to get the last line (indicated by "-1") of the resulting set.





예제 4

C:\PS>Get-Content .\DataSets\*.csv -Delimiter "*---*" -Force -Encoding UTF8

Description
-----------
This command gets the contents of all CSV files in the DataSets subdirectory. It uses the Force parameter to get all files, including hidden files, and the Encoding parameter to specify the file encoding. 

The command also uses the Delimiter parameter to divide the returned content into sets,  each of which ends at the CSV file row that contains the "*----*" marker.





예제 5

C:\PS>Get-Content .\Copy-Scripts.ps1 -Stream Zone.Identifier

[ZoneTransfer]
ZoneId=3

Description
-----------
This command uses the Stream parameter to get the content of the Zone.Identifier alternate data stream. The output includes Zone ID value of 3, which represents the Internet.

The Stream parameter is introduced in Windows PowerShell 3.0.





예제 6

C:\PS>$Manifest = (Get-Module -List PSScheduledJob).Path

C:\PS>$Hash = Invoke-Expression (Get-Content $Manifest -Raw)

C:\PS>$Hash

Name                           Value
----                           -----
Copyright                      © Microsoft Corporation. All rights reserved.
ModuleToProcess                Microsoft.PowerShell.ScheduledJob.dll
FormatsToProcess               PSScheduledJob.Format.ps1xml
PowerShellVersion              3.0
CompanyName                    Microsoft Corporation
GUID                           50cdb55f-5ab7-489f-9e94-4ec21ff51e59
Author                         Microsoft Corporation
CLRVersion                     4.0
CmdletsToExport                {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-JobTrigger...}
TypesToProcess                 PSScheduledJob.types.ps1xml
HelpInfoURI                    https://go.microsoft.com/fwlink/?LinkID=223911
ModuleVersion                  1.0.0.0

C:\PS>$Hash.ModuleToProcess
Microsoft.PowerShell.ScheduledJob.dll

Description
-----------
The commands in this example get the contents of a module manifest file (.psd1) as a hash table. The manifest file contains a hash table, but if you get the contents without the Raw dynamic parameter, it is returned as an array of newline-delimited strings.

The Raw dynamic parameter is introduced in Windows PowerShell 3.0.

The first command uses the Path property of modules to get the path to the file that contains the module manifest for the PSScheduledJob module. It saves the path in the $Manifest variable.

The second command uses the Invoke-Expression cmdlet to run a Get-Content command and the Raw dynamic parameter of the Get-Content cmdlet to get the contents of the module manifest file in a single string. The command saves the hash table in the $Hash variable.

The third command gets the hash table in the Hash variable. The contents is returned as a collection of name-value pairs.

The fourth command uses the ModuleToProcess property of the hash table to get the value of the ModuleToProcess key in the module manifest.





예제 7

C:\PS>$a = Get-Content -Path .\Download.zip -Encoding Byte -ReadCount 0

Set-Content -Path \\Server\Share\Download.zip -Encoding Byte -Value $a


$b = Get-Content -Path .\Download.zip -Encoding Byte
Set-Content -Path \\Server\Share\Download.zip -Encoding Byte -Value $b

Set-Content : Cannot proceed with byte encoding. When using byte encoding the content must be of type byte.
At line:1 char:1
+ Set-Content \\Server\Share\Download.zip -Encoding Byte -Value $b 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Content], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetContentCommand

Description
-----------
This example shows how to use the ReadCount parameter of the Get-Content cmdlet with a value of 0 to avoid byte-related errors when using the Set-Content cmdlet to write the bytes to a file.

When getting the content of a file in bytes, Get-Content creates an object (PSObject) for the bytes in each read operation. If you read the bytes one at a time, which is the default, Get-Content creates an object for each byte. The objects cause errors when you use the Set-Content cmdlet to write the bytes to a file.

The first command uses the Get-Content cmdlet to get the contents of the Download.zip file and save it in the $a variable. The command uses the Encoding dynamic parameter with a value of Byte. It also uses the ReadCount parameter with a value of 0, which directs Get-Content to get the file contents in a single read operation. The default value of the ReadCount parameter, 1, gets one byte at a time.

The second command uses the Set-Content cmdlet to write the bytes in the $a variable to the Download.zip file on a file share. The command succeeds.

The third and fourth commands show the same sequence without the ReadCount parameter. 

The third command uses the Encoding dynamic parameter of the Get-Content cmdlet to get the contents of the Download.zip file and save it in the $b variable. Because the command omits the ReadCount parameter, it uses the default value of 1.

The fourth command uses the Set-Content cmdlet to write the bytes in the $b variable to the Download.zip file on a file share. Because the content is a collection of objects, rather than a single object that contains a byte array, the command fails.





See Also

Concepts

파일 시스템 공급자

Other Resources

Clear-Content
Get-Content
Get-ChildItem
Get-Content
Get-Item
Remove-Item
Set-Content
Test-Path