首页 > 解决方案 > 将相同的字符串附加到PowerShell中CSV列中的所有变量

问题描述

我需要有关如何将字符串添加-Config到名称列中的每个名称的帮助。

CSV 文件包含:

FullName             Name    LastWriteTime
\\remotecomputer\    Henry   4/30/2020  3:44:57 PM
\\remotecompter\     Magy    12/7/2020  9:04:28 PM

所需的输出:

FullName             Name    LastWriteTime
\\remotecomputer\    Henry-Config   4/30/2020  3:44:57 PM
\\remotecompter\     Magy-Config    12/7/2020  9:04:28 PM

我当前的代码:

$InvoiceList = Import-CSV -Path C:\$source-PREP.csv | foreach($list in $InvoiceList){
$Name=$($list.name)
Get-ChildItem -Path 'C:\Names' -Filter *.txt | where {$_.Name -like '*' }|Copy-Item -Destination 'c:\newfolder' -Force }

标签: powershellfor-loopreplaceeach

解决方案


您只需要迭代 CSV 中的每一行并更新 name 属性。完成后导出回 CSV

$csvfile = 'some\path\to\file.csv'

$csvdata = Import-Csv $csvfile

$csvdata | ForEach-Object {$_.name = $_.name + '-Config'}

$csvdata | Export-Csv $csvfile -NoTypeInformation

推荐阅读