Import-Csv
從逗號分隔值 (CSV) 檔案中的項目創建類似表的自定義物件。
語法
Delimiter (預設值)
Import-Csv
[[-Path] <string[]>]
[[-Delimiter] <char>]
[-LiteralPath <string[]>]
[-Header <string[]>]
[-Encoding <string>]
[<CommonParameters>]
UseCulture
Import-Csv
[[-Path] <string[]>]
-UseCulture
[-LiteralPath <string[]>]
[-Header <string[]>]
[-Encoding <string>]
[<CommonParameters>]
Description
Import-Csv cmdlet 會從 CSV 檔案中的項目建立類似表格的自定義物件。 CSV 檔案中的每個資料行都會成為自訂物件的屬性,而列中的內容會變成屬性值。
Import-Csv 適用於任何 CSV 檔案,包括由 Export-Csv Cmdlet 產生的檔案。
您可以使用 Import-Csv Cmdlet 的參數來指定資料欄標題列和項目分隔符,或者指定 Import-Csv 使用當前文化特性的清單分隔符作為項目分隔符。
您也可以使用 ConvertTo-Csv 和 ConvertFrom-Csv Cmdlet,將對象轉換成 CSV 字串(和返回)。 這些 cmdlet 與 Export-CSV 和 Import-Csv cmdlet 相同,只是它們不處理檔。
如果 CSV 檔案中的標頭列項目包含空值或 Null 值,PowerShell 會插入預設的標頭列名稱,並顯示警告訊息。
Import-Csv 會使用位元組順序標記 (BOM) 來偵測檔案的編碼格式。 如果檔案沒有 BOM,則會假設編碼方式為 UTF8。
範例
範例 1:匯入進程物件
此範例示範如何匯出並匯入進程物件的 CSV 檔案。
Get-Process | Export-Csv -Path .\Processes.csv
$P = Import-Csv -Path .\Processes.csv
$P | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
BasePriority NoteProperty string BasePriority=8
Company NoteProperty string Company=Microsoft Corporation
...
$P | Format-Table
Name SI Handles VM WS PM NPM Path
---- -- ------- -- -- -- --- ----
ApplicationFrameHost 4 407 2199293489152 15884288 15151104 23792 C:\WINDOWS\system32\ApplicationFrameHost.exe
...
wininit 0 157 2199112204288 4591616 1630208 10376
winlogon 4 233 2199125549056 7659520 2826240 10992 C:\WINDOWS\System32\WinLogon.exe
WinStore.App 4 846 873435136 33652736 26607616 55432 C:\Program Files\WindowsApps\Microsoft.WindowsStore_11712.1001.13.0_x64__8weky...
WmiPrvSE 0 201 2199100219392 8830976 3297280 10632 C:\WINDOWS\system32\wbem\wmiprvse.exe
WmiPrvSE 0 407 2199157727232 18509824 12922880 16624 C:\WINDOWS\system32\wbem\wmiprvse.exe
WUDFHost 0 834 2199310204928 51945472 87441408 24984 C:\Windows\System32\WUDFHost.exe
Get-Process Cmdlet 會將程序物件向下傳送至 Export-Csv。
Export-Csv Cmdlet 會將進程物件轉換成 CSV 字串,並將字串儲存在 Processes.csv 檔案中。
Import-Csv Cmdlet 會從 Processes.csv 檔案匯入 CSV 字串。
字串會儲存在 $P 變數中。
$P 變數會向下傳送至 Get-Member Cmdlet,以顯示匯入 CSV 字串的屬性。
$P 變數會向下傳送至 Format-Table Cmdlet,並顯示物件。
範例 2:指定分隔符
此範例示範如何使用 Cmdlet 的 Import-Csv 參數。
Get-Process | Export-Csv -Path .\Processes.csv -Delimiter :
$P = Import-Csv -Path .\Processes.csv -Delimiter :
$P | Format-Table
Get-Process Cmdlet 會將處理程序物件沿著管線傳送至 Export-Csv。
Export-Csv Cmdlet 會將進程物件轉換成 CSV 字串,並將字串儲存在 Processes.csv 檔案中。
Delimiter 參數可用來指定冒號分隔符。
Import-Csv Cmdlet 會從 Processes.csv 檔案匯入 CSV 字串。 字串會儲存在 $P 變數中。
$P 變數被傳送到管線下游至 Format-Table cmdlet。
範例 3:指定分隔符的目前文化特性
此範例示範如何使用 Import-Csv Cmdlet 搭配 UseCulture 參數。
(Get-Culture).TextInfo.ListSeparator
Get-Process | Export-Csv -Path .\Processes.csv -UseCulture
Import-Csv -Path .\Processes.csv -UseCulture
Get-Culture Cmdlet 使用巢狀屬性 TextInfo 和 ListSeparator 來取得目前文化特性的預設清單分隔符。
Get-Process Cmdlet 會將處理程序物件沿著管線傳送至 Export-Csv。
Export-Csv Cmdlet 會將進程物件轉換成 CSV 字串,並將字串儲存在 Processes.csv 檔案中。
UseCulture 參數使用當前文化設定的預設列表分隔符。
Import-Csv Cmdlet 會從 Processes.csv 檔案匯入 CSV 字串。
範例 4:變更匯入物件中的屬性名稱
這個範例示範如何使用 的 Import-Csv 參數來變更所產生匯入物件中的屬性名稱。
Start-Job -ScriptBlock { Get-Process } | Export-Csv -Path .\Jobs.csv -NoTypeInformation
$Header = 'State', 'MoreData', 'StatusMessage', 'Location', 'Command', 'StateInfo', 'Finished',
'InstanceId', 'Id', 'Name', 'ChildJobs', 'BeginTime', 'EndTime', 'JobType', 'Output', 'Error',
'Progress', 'Verbose', 'Debug', 'Warning', 'Information'
# Delete the default header from file
$A = Get-Content -Path .\Jobs.csv
$A = $A[1..($A.Count - 1)]
$A | Out-File -FilePath .\Jobs.csv
$J = Import-Csv -Path .\Jobs.csv -Header $Header
$J
State : Running
MoreData : True
StatusMessage :
Location : localhost
Command : Get-Process
StateInfo : Running
Finished : System.Threading.ManualResetEvent
InstanceId : a259eb63-6824-4b97-a033-305108ae1c2e
Id : 1
Name : Job1
ChildJobs : System.Collections.Generic.List`1[System.Management.Automation.Job]
BeginTime : 12/20/2018 18:59:57
EndTime :
JobType : BackgroundJob
Output : System.Management.Automation.PSDataCollection`1[System.Management.Automation.PSObject]
Error : System.Management.Automation.PSDataCollection`1[System.Management.Automation.ErrorRecord]
Progress : System.Management.Automation.PSDataCollection`1[System.Management.Automation.ProgressRecord]
Verbose : System.Management.Automation.PSDataCollection`1[System.Management.Automation.VerboseRecord]
Debug : System.Management.Automation.PSDataCollection`1[System.Management.Automation.DebugRecord]
Warning : System.Management.Automation.PSDataCollection`1[System.Management.Automation.WarningRecord]
Information : System.Management.Automation.PSDataCollection`1[System.Management.Automation.InformationRecord]
Start-Job Cmdlet 會啟動執行 Get-Process的背景作業。 作業物件會經由管線被送到 Export-Csv cmdlet,並被轉換為 CSV 字串。
NoTypeInformation 參數從 CSV 輸出中刪除類型資訊標頭,並且在 PowerShell Core 中是可選的。
$Header 變數包含取代下列預設值的自定義標頭:HasMoreData、JobStateInfo、PSBeginTime、PSEndTime和 PSJobTypeName。
$A 變數會使用 Get-Content Cmdlet 從 Jobs.csv 檔案取得 CSV 字串。
$A 變數可用來從檔案中移除預設標頭。
Out-File Cmdlet 會將新版本的 Jobs.csv 檔案儲存在 $A 變數中。
Import-Csv Cmdlet 會匯入 Jobs.csv 檔案,並使用 Header 參數來套用 $Header 變數。
$J 變數包含匯入 PSCustomObject,並在 PowerShell 控制台中顯示物件。
範例 5:使用 CSV 檔案建立自定義物件
此範例示範如何使用 CSV 檔案在 PowerShell 中建立自定義物件。
Get-Content -Path .\Links.csv
113207,about_Aliases
113208,about_Arithmetic_Operators
113209,about_Arrays
113210,about_Assignment_Operators
113212,about_Automatic_Variables
113213,about_Break
113214,about_Command_Precedence
113215,about_Command_Syntax
144309,about_Comment_Based_Help
113216,about_CommonParameters
113217,about_Comparison_Operators
113218,about_Continue
113219,about_Core_Commands
113220,about_Data_Section
$A = Import-Csv -Path .\Links.csv -Header 'LinkID', 'TopicTitle'
$A | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
LinkID NoteProperty string LinkID=113207
TopicTitle NoteProperty string TopicTitle=about_Aliases
$A | Where-Object -Property TopicTitle -Like '*alias*'
LinkID TopicTitle
------ ----------
113207 about_Aliases
若要建立您的 Links.csv 檔案,請使用 Get-Content 輸出中顯示的值。
Get-Content Cmdlet 會顯示 Links.csv 檔案。
Import-Csv Cmdlet 會匯入 Links.csv 檔案。
Header 參數指定屬性名稱 LinkId 和 TopicTitle。 物件會儲存在 $A 變數中。
Get-Member Cmdlet 指令會顯示來自 標題 參數的屬性名稱。
Where-Object Cmdlet 會使用包含 別名的 TopicTitle 屬性來選取物件。
示例 6:導入缺少值的 CSV
此範例示範當 CSV 檔案中的標頭數據列包含 Null 或空白值時,PowerShell 中的 Import-Csv Cmdlet 如何回應。
Import-Csv 取代遺漏標頭數據列的預設名稱,成為 Import-Csv 傳回之對象的屬性名稱。
Get-Content -Path .\Projects.csv
ProjectID,ProjectName,,Completed
13,Inventory,Redmond,True
440,,FarEast,True
469,Marketing,Europe,False
Import-Csv -Path .\Projects.csv
WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.
ProjectID ProjectName H1 Completed
--------- ----------- -- ---------
13 Inventory Redmond True
440 FarEast True
469 Marketing Europe False
(Import-Csv -Path .\Projects.csv).H1
WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.
Redmond
FarEast
Europe
要創建 Projects.csv 檔,請使用範例 Get-Content 輸出中顯示的值。
cmdlet Get-Content 顯示 Projects.csv 檔。 標頭數據列遺漏了 ProjectName 與 Completed之間的值。 cmdlet Import-Csv 導入 Projects.csv 文件並顯示警告消息,因為 H1 是預設標頭名稱。 該 (Import-Csv -Path .\Projects.csv).H1 命令獲取 H1 屬性值並顯示警告。
參數
-Delimiter
指定分隔符,分隔 CSV 檔案中的屬性值。 預設值為逗號 (,)。
輸入一個字元,如冒號 (:)。 要指定分號 (;)請用單引號將其括起來。
如果在檔中指定實際字串分隔符以外的字元, Import-Csv 則無法從 CSV 字串建立物件,並且將返回 CSV 字串。
參數屬性
| 類型: | Char |
| 預設值: | comma (,) |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Delimiter
| Position: | 1 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Encoding
指定目標檔案的編碼類型。 預設值為 Default。
此參數可接受的值如下:
- ASCII 使用 ASCII (7 位) 字元集。
- BigEndianUnicode 使用 UTF-16 與 big-endian 位元組順序。
- 預設 使用對應至系統使用中代碼頁的編碼方式(通常是 ANSI)。
- OEM 使用對應至系統目前 OEM 代碼頁的編碼方式。
- Unicode 使用 UTF-16 搭配位元組順序。
- UTF7 使用 UTF-7。
- UTF8 使用 UTF-8。
- UTF32 使用 UTF-32 搭配位元組順序。
參數屬性
| 類型: | Encoding |
| 預設值: | Default |
| 接受的值: | ASCII, BigEndianUnicode, Default, OEM, Unicode, UTF7, UTF8, UTF32 |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Header
指定匯入檔案的替代欄位標題列。 欄標題決定了Import-Csv所建立物件的屬性名稱。
以逗號分隔的清單形式輸入列標題。 不要將標頭字串括在引號中。 以單引號括住每個欄位標題。
如果您輸入的欄位標題數量少於數據欄數量,其餘的數據欄將會被捨棄。 如果您輸入的數據行標頭數目超過數據行,則會使用空白數據行來建立其他數據行標頭。
使用 Header 參數時,請從 CSV 檔案中刪除原始標頭數據列。 否則,Import-Csv 會從標題行中的項目創建額外的物件。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-LiteralPath
指定要匯入的 CSV 檔案路徑。 不同於 Path,LiteralPath 參數的值完全按照輸入的方式使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSPath |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Path
指定要匯入的 CSV 檔案路徑。
您也可以使用管道將路徑傳送至 Import-Csv。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | False |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-UseCulture
使用針對目前文化環境的清單分隔符作為項目分隔符。 若要尋找文化特性的清單分隔符,請使用下列命令:(Get-Culture).TextInfo.ListSeparator。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
UseCulture
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
String
您可以使用管線將包含路徑的字串傳送至 Import-Csv。
輸出
Object
此 Cmdlet 會傳回 CSV 檔案中內容所描述的物件。
備註
由於導入的對像是物件類型的 CSV 版本,因此用於格式化物件類型的非 CSV 版本的PowerShell 類型格式條目無法識別它們並對其進行格式化。
Import-Csv 命令的結果是構成類似數據表的自定義物件的字串集合。 每個數據列都是個別的字串,因此您可以使用 物件的 Count 屬性來計算數據表數據列。 列是物件的屬性,而行中的項目是屬性值。
欄位標題列決定欄位數目和欄位名稱。 欄位名稱也是物件屬性的名稱。 除非您使用 Header 參數來指定列標題,否則第一行會被解讀為列標題。 如果有任何數據列的值超過標頭數據列,則會忽略其他值。
如果資料行的標題列中缺少值或包含空值或空白值,Import-Csv 會使用 H,並接著以數字表示遺漏的欄位標題和屬性名稱。
在 CSV 檔中,每個物件都由物件的屬性值的逗號分隔清單表示。 使用物件的 ToString() 方法將屬性值轉換為字串,因此它們由屬性值的名稱表示。
Export-Csv 不會匯出 物件的方法。