您提到使用 a PSCustomObject
,那么为什么不使用它呢?
$R_Input = Read-Host -Prompt "Exclude extension! `nEnter File Name"
$R_Input = $R_Input -split ","
[PSCustomObject]@{
"First File Name" = $R_Input[0]
"Second File Name" = $R_Input[1]
} | Export-Csv -Path "c:\user.csv" -NoTypeInformation -Force -Append
<#Input
Exclude extension!
Enter File Name: Path Number One, Path Number two
#>
<#OutPut
PS C:\WINDOWS\system32> Import-Csv c:\user.csv
First File Name Second File Name
--------------- ----------------
Name Number One Name Number two
#>
编辑:
如果您不关心文件名在同一列中,则可以使用for
循环遍历所有输入(用逗号分隔),并传递到PSCustomObject
. 现在,他们输入了多少名称并不重要。
$R_Input = Read-Host -Prompt "Exclude extension! `nEnter File Name"
$R_Input = $R_Input.Trim() -split ","
for ($i=0; $i -lt $R_Input.Count; $i++) {
[PSCustomObject]@{
"File Name" = $R_Input[$i]
} | Export-Csv -Path "c:\user.csv" -NoTypeInformation -Force -Append
}