FileSystem の Get-Content

適用対象: 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 コマンドレットを使用し、コマンド ラインで表示するコンテンツを取得したり、処理のために変数に保存したり、別のファイルに書き込んだりできます。フォルダーでは無効です。

注記:このカスタム コマンドレット ヘルプ ファイルは、ファイル システム ドライブにおける Get-Content コマンドレットの動作について説明します。すべてのドライブにおける Get-Content コマンドレットに関する詳細については、「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:エンコード タイプが不明か無効です。データはバイナリとして処理できます。

Encoding は FileSystem プロバイダーが Get-Content コマンドレットに追加する動的パラメーターです。このパラメーターはファイル システム ドライブでのみ機能します。

バイナリ ファイルから読み込み、バイナリ ファイルに書き込むとき、Encoding 動的パラメーターの値に Byte を指定し、ReadCount パラメーターの値に 0 を指定します。ReadCount 値が 0 であれば、1 回の読み込み操作でファイル全体が読み込まれ、1 つのオブジェクト (PSObject) に変換されます。既定の ReadCount 値 1 の場合、各読み込み操作で 1 バイトが読み込まれ、個別のオブジェクトに各バイトが変換されます。それが Set-Content コマンドレットを使用してバイトをファイルに書き込むときにエラーを発生させます。詳細については、例を参照してください。

必須?

false

位置は?

名前付き

既定値

ASCII

パイプライン入力を許可する

false

ワイルドカード文字を許可する

false

-Delimiter <文字列>

Get-Content で使用され、読み込み中にファイルをオブジェクトに分割する区切り文字を指定します。

既定値は行末文字を表す「\n」です。

そのため、既定値では、テキスト ファイルを読み込むとき、Get-Content はそれぞれが行末文字で終わる一連の文字列オブジェクトを返します。

ファイルに存在しない区切り文字を入力すると、Get-Content はファイル全体を 1 つの区切りのないオブジェクトとして返します。

このパラメーターを使用し、大きなファイルを小さなファイルに分割します。「End of Example」のようなファイル区切り記号を区切り文字として指定します。区切り文字は予約されています (破棄されません)。各ファイル セクションの最後の項目になります。

Delimiter は FileSystem プロバイダーが Get-Content コマンドレットに追加する動的パラメーターです。このパラメーターはファイル システム ドライブでのみ機能します。

トラブルシューティング上の注意:現在のところ、Delimiter パラメーターの値が空の文字列のとき、Get-Content は何も返しません。これは既知の問題です。ファイル全体を 1 つの区切りのない文字列として返すように Get-Content に強制するには、ファイルに存在しない値を入力します。

必須?

false

位置は?

名前付き

既定値

行末文字

パイプライン入力を許可する

false

ワイルドカード文字を許可する

false

-Force

非表示ファイルを含む、すべてのファイルのコンテンツを取得します。既定では、Get-Content は名前で非表示ファイルを指定しない限り非表示ファイルのコンテンツを取得しません。

必須?

false

位置は?

名前付き

既定値

False

パイプライン入力を許可する

false

ワイルドカード文字を許可する

false

-Raw <スイッチ>

改行文字を無視し、1 つの文字列でファイルの全内容を返します。既定では、ファイルの内容は改行文字で区切られる文字列の配列として返されます。

Raw は FileSystem プロバイダーが Get-Content コマンドレットに追加する動的パラメーターです。このパラメーターはファイル システム ドライブでのみ機能します。

このパラメーターは、Windows PowerShell 3.0 で導入されました。

必須?

false

位置は?

名前付き

既定値

パイプライン入力を許可する

false

ワイルドカード文字を許可する

false

-Wait

コマンド プロンプトを返す前に、コマンドレットがコンテンツを取得するのを待ちます。待機中に、Get-Content は 1 秒おきにファイルをチェックします。これは CTRL+C を押すなどして中断するまで続きます。

Wait は FileSystem プロバイダーが Get-Content コマンドレットに追加する動的パラメーターです。このパラメーターはファイル システム ドライブでのみ機能します。

必須?

false

位置は?

名前付き

既定値

False

パイプライン入力を許可する

false

ワイルドカード文字を許可する

false

-Stream <文字列>

指定した代替 NTFS ファイル ストリームの内容をファイルから取得します。ストリーム名を入力します。ワイルド カードはサポートされていません。

Stream は FileSystem プロバイダーが Get-Content コマンドレットに追加する動的パラメーターです。このパラメーターはファイル システム ドライブでのみ機能します。

このパラメーターは、Windows PowerShell 3.0 で導入されました。

必須?

false

位置は?

名前付き

既定値

パイプライン入力を許可する

false

ワイルドカード文字を許可する

false

-UseTransaction

アクティブなトランザクションのコマンドが含まれます。このパラメーターは、トランザクションが進行中の場合のみ有効です。詳細については、「about_Transactions」を参照してください。

必須?

false

位置は?

名前付き

既定値

パイプライン入力を許可する

false

ワイルドカード文字を許可する

false

<CommonParameters>

このコマンドレットは次の共通パラメーターをサポートします。-Debug、-ErrorAction、-ErrorVariable、-OutBuffer、-OutVariable、-Verbose、-WarningAction、-WarningVariable。詳細については、「about_CommonParameters」を参照してください。

入力と出力

入力型は、コマンドレットにパイプできるオブジェクトの型です。戻り値の型は、コマンドレットによって返されるオブジェクトの型です。

入力

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

読み取り回数、合計数、パス、資格情報を Get-Content にパイプで送ることができます。

出力

System.Object, System.String

Get-Content を実行すると、取得するコンテンツを表すオブジェクトが返されます。オブジェクトの種類はコンテンツの種類によって変わります。Stream パラメーターを使用する場合、このコマンドレットは代替データ ストリーム コンテンツを文字列として返します。

例 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

FileSystem プロバイダー

Other Resources

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