Get-Content
取得指定位置的項目內容。
語法
Path (預設值)
Get-Content
[-Path] <string[]>
[-ReadCount <long>]
[-TotalCount <long>]
[-Tail <int>]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-UseTransaction]
[-Delimiter <string>]
[-Wait]
[-Raw]
[-Encoding <FileSystemCmdletProviderEncoding>]
[-Stream <string>]
[<CommonParameters>]
LiteralPath
Get-Content
-LiteralPath <string[]>
[-ReadCount <long>]
[-TotalCount <long>]
[-Tail <int>]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Force]
[-Credential <pscredential>]
[-UseTransaction]
[-Delimiter <string>]
[-Wait]
[-Raw]
[-Encoding <FileSystemCmdletProviderEncoding>]
[-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 檔案。 變數 $_ 代表陣列值,因為每個物件都會在管線下傳送。
Get-Content Cmdlet 會使用 Path 參數來指定 LineNumbers.txt 檔案,並在 PowerShell 控制台中顯示內容。
範例 2:限制傳回 Get-Content 行數
此命令會取得檔案的前五行。
TotalCount 參數是用來取得前五行的內容。 此範例會使用範例 1 中建立 LineNumbers.txt 檔案。
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 行的內容。 此範例會使用範例 1 中建立 LineNumbers.txt 檔案。
(Get-Content -Path .\LineNumbers.txt -TotalCount 25)[-1]
This is Line 25
Get-Content 命令會包裝在括弧中,讓命令在前往下一個步驟之前完成。
Get-Content傳回行陣列,這可讓您在括弧後面加入索引表示法,以擷取特定的行號。 在此情況下,[-1] 索引會指定傳回的 25 行陣列中的最後一個索引。
範例 4:取得文字文件的最後一行
此命令會從檔案取得第一行和最後一行的內容。 此範例會使用範例 1 中建立 LineNumbers.txt 檔案。
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 數據流擷取數據。
Streams 可用來儲存隱藏的數據,例如屬性、安全性設定或其他數據。
範例 6:取得原始內容
此範例中的命令會以一個字串的形式取得檔案的內容,而不是字串陣列。 根據預設,若沒有 Raw 動態參數,內容會以換行符分隔字串的陣列傳回。 此範例會使用範例 1 中建立 LineNumbers.txt 檔案。
$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
您可以指定 Get-Content Cmdlet 的篩選條件。 使用篩選條件來限定 Path 參數時,您必須包含尾端星號 (*) 以指出路徑的內容。
下列命令會取得 *.log 目錄中所有 C:\Temp 檔案的內容。
Get-Content -Path C:\Temp\* -Filter *.log
範例 8:以位元組陣列的形式取得檔案內容
此範例示範如何將檔案的內容轉換為 [byte[]],作為單一物件取得。
$byteArray = Get-Content -Path C:\temp\test.txt -Encoding Byte -Raw
Get-Member -InputObject $bytearray
TypeName: System.Byte[]
Name MemberType Definition
---- ---------- ----------
Count AliasProperty Count = Length
Add Method int IList.Add(System.Object value)
第一個命令會使用 Encoding 參數,從檔案取得位元組數據流。
Raw 參數可確保以 [System.Byte[]]格式傳回位元組。 如果 Raw 參數不存在,則傳回值是位元組資料流,PowerShell 會將它解譯為 [System.Object[]]。
參數
-Credential
備註
任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command。
參數屬性
| 類型: | PSCredential |
| 預設值: | Current user |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Delimiter
指定 Get-Content 在讀取時用來將檔案分割成物件的分隔符。 默認值為 \n,也就是行尾字元。 讀取文字檔時,Get-Content 會傳回字串物件的集合,每個物件都是以行尾字元結尾。 當您輸入不存在於檔案中的分隔符時,Get-Content 會將整個檔案當做單一未匯入的物件傳回。
您可以使用此參數,將大型檔案分割成較小的檔案,方法是將檔案分隔符指定為作為分隔符。 分隔符會被保留(不捨棄),並成為每個檔案部分中的最後一個項目。
Delimiter 是 FileSystem 提供者新增至 Get-Content Cmdlet 的動態參數。 此參數僅適用於檔案系統磁碟驅動器。
備註
目前,當 Delimiter 參數的值是空字串時,Get-Content 不會傳回任何結果。 這是已知問題。 若要強制 Get-Content 傳回整個檔案作為單一未分隔的字串。 輸入不存在於檔案中的值。
參數屬性
| 類型: | String |
| 預設值: | End-of-line character |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Encoding
指定目標檔案的編碼類型。 預設值為 Default。
此參數可接受的值如下:
- ASCII 使用 ASCII (7 位) 字元集。
- BigEndianUnicode 使用 UTF-16 與 big-endian 位元組順序。
- BigEndianUTF32 使用 UTF-32 搭配 big-endian 位元組順序。
- 位元組 將一組字元編碼成位元組序列。
- 預設 使用對應至系統使用中代碼頁的編碼方式(通常是 ANSI)。
- OEM 使用對應至系統目前 OEM 代碼頁的編碼方式。
- 字串 與 unicode 相同。
- Unicode 使用 UTF-16 搭配位元組順序。
未知 與 Unicode相同。 - UTF7 使用 UTF-7。
- UTF8 使用 UTF-8。
- UTF32 使用 UTF-32 搭配位元組順序。
編碼是 FileSystem 提供者新增至 Get-Content Cmdlet 的動態參數。
此參數僅適用於檔案系統磁碟驅動器。
讀取和寫入二進位檔時,請使用 Set-Content Cmdlet 將位元組寫入檔案時發生錯誤。
參數屬性
| 類型: | FileSystemCmdletProviderEncoding |
| 預設值: | Default |
| 接受的值: | ASCII, BigEndianUnicode, BigEndianUTF32, Byte, Default, OEM, String, Unicode, Unknown, UTF7, UTF8, UTF32 |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Exclude
指定要在此 Cmdlet 作業中排除的項目或多項,並以字串陣列形式表示。 此參數的值對 路徑 參數進行限定。
輸入路徑元素或模式,例如 *.txt。
允許使用通配符字元。
只有在命令包含項目的內容,例如 時,C:\Windows\* 參數才有效,其中通配符會指定 C:\Windows 目錄的內容。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Filter
指定篩選條件,以限定 Path 參數。 FileSystem 提供者是唯一已安裝且支援使用篩選的 PowerShell 提供者。 您可以在 about_Wildcards中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Force
Force 會覆寫唯讀屬性或建立目錄以完成檔案路徑。 Force 參數不會嘗試變更檔案許可權或覆寫安全性限制。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Include
指定此 Cmdlet 在操作中包含的一個或多個專案,這些專案是以字串陣列的形式表示。 此參數的值對 路徑 參數進行限定。 輸入路徑元素或模式,例如 "*.txt"。 允許使用通配符字元。 只有當命令包含項目的內容,例如 時,C:\Windows\* 參數才有效,其中通配符會指定 C:\Windows 目錄的內容。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-LiteralPath
指定通往一個或多個位置的路徑。 LiteralPath 的值會按原本輸入的方式使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
如需詳細資訊,請參閱 about_Quoting_Rules。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSPath |
參數集
LiteralPath
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Path
指定一個項目的路徑,該項目由 Get-Content 取得內容。 允許使用通配符字元。 路徑必須是項目的路徑,而不是容器的路徑。 例如,您必須指定一或多個檔案的路徑,而不是目錄的路徑。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
Path
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Raw
忽略換行符,並在一個字串中傳回保留換行符的整個檔案內容。 根據預設,檔案中的換行符會做為分隔符,將輸入分隔成字元串陣列。 此參數是在 PowerShell 3.0 中引進的。
Raw 是 FileSystem 提供者新增至 Get-Content Cmdlet 的動態參數。此參數只適用於文件系統磁碟驅動器。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-ReadCount
指定一次透過管線傳送多少行內容。 預設值為 1。 值為 0 (零) 一次傳送所有內容。
此參數不會變更顯示的內容,但會影響顯示內容所需的時間。 當 ReadCount 的值增加時,傳回第一行所需的時間會增加,但整個作業所需的總時間卻會減少。 這在大型專案中可能會產生明顯的差異。
參數屬性
| 類型: | Int64 |
| 預設值: | 1 |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Stream
從檔案取得指定之替代NTFS檔案數據流的內容。 輸入數據流名稱。 不支援通配符。
Stream 是 FileSystem 提供者新增至 Get-Content Cmdlet 的動態參數。
此參數僅適用於 Windows 系統上的文件系統磁碟驅動器。 此參數是在 Windows PowerShell 3.0 中引進的。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Tail
指定檔案結尾或其他項目的行數。 您可以使用參數名稱 Tail 或其別名 Last。 此參數是在 PowerShell 3.0 中引進的。
參數屬性
| 類型: | Int32 |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 最後 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-TotalCount
指定檔案或其他項目開頭的行數。 預設值為 -1 (所有行)。
您可以使用 TotalCount 參數名稱或其別名、First 或 Head。
參數屬性
| 類型: | Int64 |
| 預設值: | -1 |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 第一, 頭 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-UseTransaction
在作用中交易中包含 命令。 只有在交易進行中時,此參數才有效。 如需詳細資訊,請參閱 about_Transactions。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | usetx |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Wait
在輸出所有現有的行之後,讓檔案保持開啟。 等候時,Get-Content 每秒檢查檔案一次,並在出現時輸出新行。 您可以按 CTRL+C來中斷
Wait 是 FileSystem 提供者新增至 Get-Content Cmdlet 的動態參數。 此參數僅適用於檔案系統磁碟驅動器。
Wait 無法與 Raw結合。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
System.Int64, System.String
您可以使用管線將讀取計數、總計數、路徑或認證傳送至 Get-Content。
輸出
System.Byte, System.String
Get-Content 會傳回字串或位元組。 輸出類型取決於您指定為輸入的內容類型。
備註
Get-Content Cmdlet 的設計目的是要處理任何提供者所公開的數據。 若要在您的會話中取得提供者,請使用 Get-PSProvider Cmdlet。 如需詳細資訊,請參閱 about_Providers。