Get-Content
在指定的位置取得項目的內容。
Syntax
Get-Content
[-ReadCount <Int64>]
[-TotalCount <Int64>]
[-Tail <Int32>]
[-Path] <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Force]
[-Credential <PSCredential>]
[-Delimiter <String>]
[-Wait]
[-Raw]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <String>]
[<CommonParameters>]
Get-Content
[-ReadCount <Int64>]
[-TotalCount <Int64>]
[-Tail <Int32>]
-LiteralPath <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Force]
[-Credential <PSCredential>]
[-Delimiter <String>]
[-Wait]
[-Raw]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <String>]
[<CommonParameters>]
Description
Get-Content
Cmdlet 會取得路徑所指定位置的項目內容,例如檔案中的文字或函式的內容。 對於檔案,內容會一次讀取一行,並傳回 物件的集合,每個物件都代表一行內容。
從 PowerShell 3.0 開始, Get-Content
也可以從項目的開頭或結尾取得指定的行數。
範例
範例 1︰取得文字檔的內容
這個範例會取得目前目錄中檔案的內容。 檔案 LineNumbers.txt
包含 100 行的格式, 這是第 X 行 ,並用於數個範例中。
1..100 | ForEach-Object { Add-Content -Path .\LineNumbers.txt -Value "This is line $_." }
Get-Content -Path .\LineNumbers.txt
This is Line 1
This is Line 2
...
This is line 99.
This is line 100.
陣列值 1-100 會從管線向下傳送至 ForEach-Object
Cmdlet。 ForEach-Object
使用腳本區塊搭配 Add-Content
Cmdlet 來建立 LineNumbers.txt
檔案。 變數 $_
代表陣列值,因為每個物件都會在管線下傳送。 Cmdlet Get-Content
會使用 Path 參數來指定檔案, LineNumbers.txt
並在 PowerShell 控制台中顯示內容。
範例 2:限制傳回 Get-Content 行數
此命令會取得檔案的前五行。 TotalCount 參數用來取得前五行的內容。 此範例會 LineNumbers.txt
使用範例 1 中建立的檔案。
Get-Content -Path .\LineNumbers.txt -TotalCount 5
This is Line 1
This is Line 2
This is Line 3
This is Line 4
This is Line 5
範例 3:從文本檔取得特定一行內容
此命令會從檔案取得特定的行數,然後只顯示該內容的最後一行。 TotalCount 參數會取得前 25 行的內容。 此範例會 LineNumbers.txt
使用範例 1 中建立的檔案。
(Get-Content -Path .\LineNumbers.txt -TotalCount 25)[-1]
This is Line 25
命令 Get-Content
會包裝在括弧中,讓命令在移至下一個步驟之前完成。 Get-Content
會傳回行陣列,這可讓您在括弧後面加入索引表示法,以擷取特定的行號。 在此情況下, [-1]
索引會指定傳回的 25 行所擷取數位中的最後一個索引。
範例 4:取得文字文件的最後一行
此命令會從檔案取得第一行和最後一行的內容。 此範例會 LineNumbers.txt
使用範例 1 中建立的檔案。
Get-Item -Path .\LineNumbers.txt | Get-Content -Tail 1
This is Line 100
此範例會 Get-Item
使用 Cmdlet 來示範您可以將檔案管線傳送至 Get-Content
參數。 Tail 參數會取得檔案的最後一行。 這個方法比擷取所有行並使用 [-1]
索引表示法快。
範例 5:取得替代數據流的內容
此範例描述如何使用 Stream 參數,取得儲存在 Windows NTFS 磁碟區上之檔案的替代數據流內容。 在此範例中 Set-Content
,Cmdlet 可用來在名為 Stream.txt
的檔案中建立範例內容。
Set-Content -Path .\Stream.txt -Value 'This is the content of the Stream.txt file'
# Specify a wildcard to the Stream parameter to display all streams of the recently created file.
Get-Item -Path .\Stream.txt -Stream *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Test\Stream.txt::$DATA
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Test
PSChildName : Stream.txt::$DATA
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
FileName : C:\Test\Stream.txt
Stream : :$DATA
Length : 44
# Retrieve the content of the primary, or $DATA stream.
Get-Content -Path .\Stream.txt -Stream $DATA
This is the content of the Stream.txt file
# Use the Stream parameter of Add-Content to create a new Stream containing sample content.
Add-Content -Path .\Stream.txt -Stream NewStream -Value 'Added a stream named NewStream to Stream.txt'
# Use Get-Item to verify the stream was created.
Get-Item -Path .\Stream.txt -Stream *
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Test\Stream.txt::$DATA
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Test
PSChildName : Stream.txt::$DATA
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
FileName : C:\Test\Stream.txt
Stream : :$DATA
Length : 44
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Test\Stream.txt:NewStream
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\Test
PSChildName : Stream.txt:NewStream
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
FileName : C:\Test\Stream.txt
Stream : NewStream
Length : 46
# Retrieve the content of your newly created Stream.
Get-Content -Path .\Stream.txt -Stream NewStream
Added a stream named NewStream to Stream.txt
Stream 參數是 FileSystem 提供者的動態參數。
根據預設 Get-Content
,只會從主要或 $DATA
數據流擷取數據。 數據流 可用來儲存隱藏的數據,例如屬性、安全性設定或其他數據。
範例 6:取得原始內容
此範例中的命令會以一個字串的形式取得檔案的內容,而不是字串陣列。 根據預設,如果沒有 Raw 動態參數,內容會以換行符分隔字串的數位傳回。 此範例會 LineNumbers.txt
使用範例 1 中建立的檔案。
$raw = Get-Content -Path .\LineNumbers.txt -Raw
$lines = Get-Content -Path .\LineNumbers.txt
Write-Host "Raw contains $($raw.Count) lines."
Write-Host "Lines contains $($lines.Count) lines."
Raw contains 1 lines.
Lines contains 100 lines.
範例 7:搭配 Get-Content 使用篩選
您可以指定 Cmdlet 的篩選條件 Get-Content
。 使用篩選來限定 Path 參數時,您必須包含尾端星號 (*
) ,以指出路徑的內容。
下列命令會取得目錄中所有 *.log
檔案 C:\Temp
的內容。
Get-Content -Path C:\Temp\* -Filter *.log
範例 8:以位元組陣列的形式取得檔案內容
這個範例示範如何以單一物件的形式 [byte[]]
取得檔案的內容。
$byteArray = Get-Content -Path C:\temp\test.txt -AsByteStream -Raw
Get-Member -InputObject $bytearray
TypeName: System.Byte[]
Name MemberType Definition
---- ---------- ----------
Count AliasProperty Count = Length
Add Method int IList.Add(System.Object value)
第一個命令會使用 AsByteStream 參數,從檔案取得位元組數據流。
Raw 參數可確保位元組會以 傳回。[System.Byte[]]
如果 Raw 參數不存在,則傳回值是位元組資料流,PowerShell 會將它解譯為 [System.Object[]]
。
參數
-AsByteStream
指定內容應讀取為位元組數據流。 AsByteStream 參數是在 Windows PowerShell 6.0 中引進。
當您搭配 Encoding 參數使用 AsByteStream 參數時,就會發生警告。 AsByteStream 參數會忽略任何編碼,而輸出會以位元組數據流的形式傳回。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Credential
注意
任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要在執行此 Cmdlet 時模擬其他使用者,或提高您的認證,請使用 Invoke-Command。
Type: | PSCredential |
Position: | Named |
Default value: | Current user |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Delimiter
指定用來在讀取檔案時將檔案分割成 物件的分隔符 Get-Content
。 預設值為 \n
,這是行尾字元。 讀取文字檔時, Get-Content
會傳回字串物件的集合,每個物件結尾都是行尾字元。 當您輸入不存在於檔案中的分隔符時, Get-Content
會將整個檔案當做單一未傳遞的物件傳回。
您可以使用此參數將大型檔案分割成較小的檔案,方法是指定檔案分隔符做為分隔符。 分隔符號會保留 (不會被捨棄),並成為每個檔案區段中的最後一個項目。
分隔符 是 FileSystem 提供者新增至 Cmdlet 的 Get-Content
動態參數。 此參數只適用於檔案系統磁碟機。
注意
目前, 當 Delimiter 參數的值是空字串時, Get-Content
不會傳回任何專案。 這是已知的問題。 若要強制 Get-Content
以單一未傳遞字串的形式傳回整個檔案。 輸入檔案中不存在的值。
Type: | String |
Position: | Named |
Default value: | End-of-line character |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Encoding
指定目標檔案的編碼類型。 預設值是 utf8NoBOM
。
此參數可接受的值如下所示:
ascii
:使用 ASCII (7 位) 字元集的編碼方式。bigendianunicode
:使用大位元組位元組順序以UTF-16格式編碼。oem
:使用 MS-DOS 和控制台程式的預設編碼方式。unicode
:使用位元組由小到大的順序,以UTF-16格式編碼。utf7
:以 UTF-7 格式編碼。utf8
:以 UTF-8 格式編碼。utf8BOM
:使用位元組順序標記 (BOM) 以 UTF-8 格式編碼utf8NoBOM
:以 UTF-8 格式編碼,不含位元組順序標記 (BOM)utf32
:以 UTF-32 格式編碼。
Encoding 是 FileSystem 提供者新增至 Cmdlet 的 Get-Content
動態參數。
此參數僅適用於檔案系統磁碟驅動器。
讀取和寫入二進位檔時,請使用 AsByteStream 參數和 ReadCount 參數的 0 值。 ReadCount 值 0 會在單一讀取作業中讀取整個檔案。 預設的 ReadCount 值 1 會讀取每個讀取作業中的一個字節,並將每個位元組轉換成個別的物件,除非您使用 AsByteStream 參數,否則當您使用 Set-Content
Cmdlet 將位元組寫入檔案時,會造成錯誤。
從 PowerShell 6.2 開始, Encoding 參數也允許已註冊代碼頁的數值標識符, (例如 -Encoding 1251
) 或已註冊代碼頁的字串名稱 (,例如 -Encoding "windows-1251"
) 。 如需詳細資訊,請參閱 Encoding.CodePage 的 .NET 檔。
Type: | Encoding |
Accepted values: | ASCII, BigEndianUnicode, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM, UTF32 |
Position: | Named |
Default value: | UTF8NoBOM |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Exclude
指定此 Cmdlet 在作業中排除的專案或專案,做為字串陣列。 此參數的值會限定 Path 參數。
輸入路徑元素或模式,例如 *.txt
。
允許使用萬用字元。
Exclude 參數只有在 命令包含項目的內容時有效,例如 C:\Windows\*
,其中通配符會指定目錄的內容 C:\Windows
。
Type: | String[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-Filter
指定篩選條件以限定 Path 參數。 FileSystem 提供者是唯一支援使用篩選的已安裝 PowerShell 提供者。 您可以在 about_Wildcards 中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。
Type: | String |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-Force
Force 會覆寫唯讀屬性或建立目錄以完成檔案路徑。 Force 參數不會嘗試變更檔案許可權或覆寫安全性限制。
Type: | SwitchParameter |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Include
以字串陣列指定此 Cmdlet 在作業中納入的項目。 此參數的值會限定 Path 參數。 輸入路徑元素或模式,例如 "*.txt"
。 允許使用萬用字元。 Include 參數只有在命令包含項目的內容時有效,例如 C:\Windows\*
,其中通配符會指定目錄的內容C:\Windows
。
Type: | String[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-LiteralPath
指定一個或多個位置的路徑。 LiteralPath 的值會完全依照其類型使用。 沒有字元會被視為萬用字元。 如果路徑包含逸出字元,請將它括在單引號中。 單引號會告訴PowerShell不要將任何字元解譯為逸出序列。
如需詳細資訊,請參閱 about_Quoting_Rules。
Type: | String[] |
Aliases: | PSPath, LP |
Position: | Named |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Path
指定取得內容之項目 Get-Content
的路徑。 允許使用萬用字元。 路徑需為項目的路徑,而非容器的路徑。 例如,您必須指定一或多個檔案的路徑,而非目錄的路徑。
Type: | String[] |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | True |
-Raw
忽略換行符,並在一個字串中傳回保留換行符的整個檔案內容。 根據預設,檔案中的換行符會當做分隔符,將輸入分隔成字元串陣列。 此參數是在 PowerShell 3.0 中引進。
Raw 是 FileSystem 提供者新增至 Get-Content
Cmdlet 的動態參數。此參數僅適用於文件系統磁碟驅動器。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-ReadCount
指定管線一次傳送多少行的內容。 預設值為 1。 值為 0 (零) 會一次傳送所有內容。
這個參數不會變更顯示的內容,但它會影響顯示內容所花費的時間。 隨著 ReadCount 的值增加,傳回第一行所花費的時間也會增加,但是作業的總時間減少。 這可讓大型專案有可辨識的差異。
Type: | Int64 |
Position: | Named |
Default value: | 1 |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Stream
從檔案取得指定的替代 NTFS 檔案資料流內容。 輸入資料流名稱。 不支援萬用字元。
Stream 是 FileSystem 提供者新增至 Cmdlet 的Get-Content
動態參數。
此參數僅適用於 Windows 系統上的文件系統磁碟驅動器。 此參數是在 Windows PowerShell 3.0 引進。
Type: | String |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Tail
指定從檔案或其他項目的結尾算起的行數。 您可以使用 Tail 參數名稱或其別名 Last。 此參數是在 PowerShell 3.0 中引進。
Type: | Int32 |
Aliases: | Last |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-TotalCount
指定從檔案或其他項目的開頭算起的行數。 預設值為 -1 (所有行)。
您可以使用 TotalCount 參數名稱或其別名 First 或 Head。
Type: | Int64 |
Aliases: | First, Head |
Position: | Named |
Default value: | -1 |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Wait
在輸出所有現有行之後,讓檔案保持開啟。 等候時, Get-Content
每秒檢查檔案一次,並在出現時輸出新行。 您可以按 CTRL+C 來中斷 Wait。 如果檔案遭到刪除,則等候也會結束,在此情況下會回報非終止錯誤。
Wait 是 FileSystem 提供者新增至 Cmdlet 的 Get-Content
動態參數。 此參數只適用於檔案系統磁碟機。 等候 無法與 Raw合併。
Type: | SwitchParameter |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
System.Int64, System.String[], System.Management.Automation.PSCredential
您可以使用管線將讀取計數、總計計數、路徑或認證傳送至 Get-Content
。
輸出
System.Byte, System.String
Get-Content
會傳回字串或位元組。 輸出類型取決於您指定為輸入的內容類型。
備註
此 Get-Content
Cmdlet 的設計目的是要處理任何提供者所公開的數據。 若要在您的會話中取得提供者,請使用 Get-PSProvider
Cmdlet。 如需詳細資訊,請參閱 about_Providers。