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
Cmdlet Get-Content
會取得路徑所指定位置的項目內容,例如檔案中的文字或函式的內容。 對於檔案,內容一次會讀取一行,並傳回 物件的集合,每個物件都代表一行內容。
從 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 stream.
# Note the single quotes to prevent variable substitution.
Get-Content -Path .\Stream.txt -Stream ':$DATA'
This is the content of the Stream.txt file
# Alternative way to get the same content.
Get-Content -Path .\Stream.txt -Stream ""
# The primary stream doesn't need to be specified to get the primary stream of the file.
# This gets the same data as the prior two examples.
Get-Content -Path .\Stream.txt
This is the content of the Stream.txt file
# Use the Stream parameter of Add-Content to create a new Stream containing sample content.
$addContentSplat = @{
Path = '.\Stream.txt'
Stream = 'NewStream'
Value = 'Added a stream named NewStream to Stream.txt'
}
Add-Content @addContentSplat
# 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 參數會忽略任何編碼,而且輸出會以位元組數據流的形式傳回。
讀取和寫入二進位檔時,請使用 AsByteStream 參數和 ReadCount 參數的 0 值。 ReadCount 值為 0 會在單一讀取作業中讀取整個檔案。 預設 的 ReadCount 值 1 會在每個讀取作業中讀取一個字節,並將每個位元組轉換成個別的物件。 除非您搭配 使用 AsByteStream 參數搭配 Set-Content
,否則管線單一位元組輸出會導致Set-Content
錯誤。
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
動態參數。 此參數只適用於檔案系統磁碟機。
注意
目前,當 分隔符 參數的值是空字串時, 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 位) 字元集的編碼方式。ansi
:針對目前文化特性的 ANSI 代碼頁使用 編碼。 此選項已在PowerShell 7.4中新增。bigendianunicode
:使用大位元組位元組順序以UTF-16格式編碼。bigendianutf32
:使用大位元組位元組順序以UTF-32格式編碼。oem
:使用 MS-DOS 和控制台程式的預設編碼方式。unicode
:使用小位元節順序以UTF-16格式編碼。utf7
:以 UTF-7 格式編碼。utf8
:以 UTF-8 格式編碼。utf8BOM
:使用位元節順序標記 (BOM) ,以 UTF-8 格式編碼utf8NoBOM
:以 UTF-8 格式編碼,不含位元組順序標記 (BOM)utf32
:以 UTF-32 格式編碼。
編碼是 FileSystem 提供者新增至 Cmdlet 的 Get-Content
動態參數。
此參數僅適用於檔案系統磁碟驅動器。
從 PowerShell 6.2 開始, Encoding 參數也允許已註冊代碼頁的數值標識符, (例如 -Encoding 1251
) 或已註冊代碼頁的字串名稱 (,例如 -Encoding "windows-1251"
) 。 如需詳細資訊,請參閱 Encoding.CodePage 的 .NET 檔。
從 PowerShell 7.4 開始,您可以使用 Ansi
Encoding 參數的值來傳遞目前文化特性 ANSI 代碼頁的數值標識碼,而不需要手動指定它。
注意
不再建議使用UTF-7* 。 自 PowerShell 7.1 起,如果您為 Encoding 參數指定utf7
,則會撰寫警告。
Type: | Encoding |
Accepted values: | ASCII, BigEndianUnicode, BigEndianUTF32, 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
注意
此參數僅適用於 Windows。
從檔案取得指定的替代 NTFS 檔案資料流內容。 輸入資料流名稱。 不支援通配符。
Stream 是 FileSystem 提供者新增至 Cmdlet 的Get-Content
動態參數。
此參數僅適用於 Windows 系統上的文件系統磁碟驅動器。
此參數是在 Windows PowerShell 3.0 引進。 在 PowerShell 7.2 中, Get-Content
可以從目錄和檔案擷取替代數據流的內容。
Type: | String |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Tail
指定從檔案或其他項目的結尾算起的行數。 您可以使用 Tail 參數名稱或其別名 Last。 負值會導致 Cmdlet 傳回整個內容。
此參數是在 PowerShell 3.0 中引進。
Type: | Int32 |
Aliases: | Last |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-TotalCount
指定從檔案或其他項目的開頭算起的行數。 負值會導致 Cmdlet 傳回整個內容。
您可以使用 TotalCount 參數名稱或其別名 First 或 Head。
Type: | Int64 |
Aliases: | First, Head |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Wait
使 Cmdlet 無限期等候,讓檔案保持開啟狀態,直到中斷為止。 等候時, Get-Content
每秒檢查檔案一次,並在出現時輸出新的行。 搭配 TotalCount 參數使用時, Get-Content
請等到指定的檔案中有指定的行數可用為止。 例如,如果您指定 TotalCount 為 10 且檔案已經有 10 個以上的行, Get-Content
則會傳回 10 行並結束。 如果檔案少於 10 行, 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 |
輸入
您可以使用管線將讀取計數或總計計數傳送至此 Cmdlet。
String[]
您可以使用管線將路徑傳送至此 Cmdlet。
您可以使用管線將認證傳送至此 Cmdlet。
輸出
當您使用 AsByteStream 參數時,此 Cmdlet 會將內容傳回為位元組。
根據預設,此 Cmdlet 會將內容傳回為字串陣列,每行一行一個。 當您使用 Raw 參數時,它會傳回包含檔案中每一行的單一字串。
備註
PowerShell 包含下列的 Get-Content
別名:
- 所有平台:
gc
type
- Windows:
cat
Cmdlet Get-Content
的設計目的是要處理任何提供者所公開的數據。 若要取得會話中的提供者,請使用 Get-PSProvider
Cmdlet。 如需詳細資訊,請參閱 about_Providers。