Home > OS >  Powershell concatenate a csv with a static string and export to a file
Powershell concatenate a csv with a static string and export to a file

Time:07-30

I've checked a few examples and similar threads. I'm getting SOME data in my export , just not either a: the format im looking for or b: i get the format correct , but no data. I want to concatenate a string to a single column in a .csv then to a string and then export to a .csv Here's what I have:

content of C:\csv\serials.csv

serialnumber
5062TBH
5032TZS
4362BTD
3256FDR
6087WSD
8761GWD
8876FGT
5062TBF
5062Tpl
5062HGD
$OrgUnit = Read-Host "enter the destination orgUnit here:"
$command = "my command string:"
$serialnumbers = Import-CSV -Path C:\CSV\serials.csv -Header serialnumber

for ( $n = 0; $n -lt $serialnumbers.Count; $n   ) {
  New-Object PSCustomObject -Property @{
    "myCommand" = $command[$n].command
    "Serial" = $serialnumbers[$n].serial
    "OrgUnit" = $OrgUnit[$n].orgunit
  } | Export-Csv C:\CSV\Command.csv -notype -Append -Force
}

the output I recieve in Command.csv:

"Serial","myCommand","OrgUnit"
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,

What I'm looking for

content of C:\csv\commands.csv

command,serialnumber,orgunit
my command string:5062TBHorgunit
my command string:5032TZSorgunit
my command string:4362BTDorgunit
my command string:3256FDRorgunit
my command string:6087WSDorgunit
my command string:8761GWDorgunit
my command string:8876FGTorgunit
my command string:5062TBForgunit
my command string:5062Tplorgunit
my command string:5062HGDorgunit

I feel like I'm close. but I want to understand what im doing more importantly than just getting it done

EDIT: Updated the suggestion below and it works! even better , I understand why!

Now, I'm trying to figure out how to modify the order of fields outputted to the .CSV

I'm getting : content from Serials.csv

"Serial","myCommand","OrgUnit"
"serialnumber","my command string","OrgunitInputFromUser"

and I'm looking for

"myCommand","Serial","OrgUnit"
"my command string","serialnumber","OrgunitInputFromUser"

I think this will give me a better understanding how to manage that output

EDIT : was able to manipulate it using Select-Object

thank you everyone for your help!

Final result:

$command = "my command string"

$OrgUnit = Read-Host "Paste the destination orgUnit here:"

$serialnumbers = Import-CSV -Path C:\CSV\serials.csv -Header serialnumber

for ( $n = 0; $n -lt $serialnumbers.Count; $n   ) {
  New-Object PSCustomObject -Property @{
    "myCommand" = $command
    "Serial" = $serialnumbers[$n].serialnumber
    "OrgUnit" = $OrgUnit
  } | Select-Object "myCommand","Serial","OrgUnit" | Export-Csv C:\CSV\Command.csv -notype -Append -Force
}

CodePudding user response:

Complementing the existing solutions, here is a refactoring for simplicity.

$command = "my command string"

$OrgUnit = Read-Host "Paste the destination orgUnit here:"

Import-CSV -Path C:\CSV\serials.csv -Header serialnumber | ForEach-Object {
    [PSCustomObject] @{
        "myCommand" = $command
        "Serial" = $_.serialnumber
        "OrgUnit" = $OrgUnit
    } 
} | Export-Csv C:\CSV\Command.csv -notype -Force
  • Using a pipeline we get rid of temporary variable $serialnumbers.
  • [PSCustomObject] @{...} is short syntax for New-Object PSCustomObject -Property @{...}
  • Moving Export-Csv to the end of the pipeline is faster than using it within a loop with -Append, which opens and closes the output file for each row, a quite expensive operation. In contrast, within the pipeline, the output file will be opened once when the pipeline starts, it will be appended to in a streaming fashion and finally be closed when the pipeline ends.

CodePudding user response:

you are quite close, just note that $command and $OrgUnit are strings, not arrays. So indexing them with $n makes no sense. Also, the $serialnumbers array has objects with a serialnumber, not a serial.

So change the three lines inside the @Property to:

"myCommand" = $command
"Serial" = $serialnumbers[$n].serialnumber
"OrgUnit" = $OrgUnit

Seems to work for me.

  • Related