Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在powershell中将行添加到CSV文件_Powershell_Csv - Fatal编程技术网

在powershell中将行添加到CSV文件

在powershell中将行添加到CSV文件,powershell,csv,Powershell,Csv,正在尝试了解如何将一行添加到带有标题的csv文件中。 我通过以下方式获取内容: $fileContent = Import-csv $file -header "Date", "Description" $File返回 Date,Description Text1,text2 text3,text4 如何使用新的日期和说明追加行。抱歉,我对powershell比较陌生。感谢所有能够帮助创建新自定义对象并将其添加到导入Csv创建的对象数组的人 $fileContent = Import-cs

正在尝试了解如何将一行添加到带有标题的csv文件中。 我通过以下方式获取内容:

$fileContent = Import-csv $file -header "Date", "Description"
$File
返回

Date,Description
Text1,text2 
text3,text4

如何使用新的日期和说明追加行。抱歉,我对powershell比较陌生。感谢所有能够帮助

创建新自定义对象并将其添加到
导入Csv
创建的对象数组的人

$fileContent = Import-csv $file -header "Date", "Description"
$newRow = New-Object PsObject -Property @{ Date = 'Text4' ; Description = 'Text5' }
$fileContent += $newRow

要简单地附加到powershell中的文件,可以使用“添加内容”

因此,要仅向文件中添加新行,请尝试以下操作,$YourNewDate和$YourDescription包含所需的值

$NewLine = "{0},{1}" -f $YourNewDate,$YourDescription
$NewLine | add-content -path $file
或者


这只会将新行标记到.csv的末尾,不适用于创建需要添加标题的新.csv文件。

我知道这是一个旧线程,但这是我在搜索时找到的第一个线程。+=解决方案对我不起作用。我开始工作的代码如下

#this bit creates the CSV if it does not already exist
$headers = "Name", "Primary Type"
$psObject = New-Object psobject
foreach($header in $headers)
{
 Add-Member -InputObject $psobject -MemberType noteproperty -Name $header -Value ""
}
$psObject | Export-Csv $csvfile -NoTypeInformation

#this bit appends a new row to the CSV file
$bName = "My Name"
$bPrimaryType = "My Primary Type"
    $hash = @{
             "Name" =  $bName
             "Primary Type" = $bPrimaryType
              }

$newRow = New-Object PsObject -Property $hash
Export-Csv $csvfile -inputobject $newrow -append -Force
我能够使用它作为一个函数来循环一系列数组,并将内容输入到CSV文件中


它在powershell 3及更高版本中工作。

对我来说很简单,如下所示:

$Time = Get-Date -Format "yyyy-MM-dd HH:mm K"
$Description = "Done on time"

"$Time,$Description"|Add-Content -Path $File # Keep no space between content variables
如果有很多列,则创建一个变量,如
$NewRow
如下:

$Time = Get-Date -Format "yyyy-MM-dd HH:mm K"
$Description = "Done on time"
$NewRow = "$Time,$Description" # No space between variables, just use comma(,).

$NewRow | Add-Content -Path $File # Keep no space between content variables

请注意文件的
设置内容
(覆盖现有内容)和
添加内容
(附加到现有内容)之间的区别。

欢迎使用StackOverflow。请在选择问题标签时更加小心。我编辑了你的,以便更适合你的问题。(标签用于按主题或主题将问题分组,而您原来的
stackoverflow.com
标签不仅不合适,而且与您的问题完全无关。)您可能还想访问以熟悉网站的总体情况。:)这对我来说非常有用,只是它改变了我专栏的顺序。在使用上述答案添加行之后,我必须使用
$Import=$Import |选择Object…
重新排序要导出的列。谢谢marceljg。我一直在寻找相同的解决方案,而您的一行代码非常适合我。只是想知道有没有一种简单的方法来预先添加行,而不是追加行?
$Time = Get-Date -Format "yyyy-MM-dd HH:mm K"
$Description = "Done on time"
$NewRow = "$Time,$Description" # No space between variables, just use comma(,).

$NewRow | Add-Content -Path $File # Keep no space between content variables