Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Export CSV object to .CSV file, Powershell

$
0
0
This seemed straightforward but I'm getting errors at the end of everything I try. In the attached script I import a CSV file and change the formatting of a certain column's values so that another script can work with it. I successfully modify the CSV
# nameFlip
# 
# changes the name formatting in a collumn of a csv saves file called "newCSV"

function main {
    # IMPORT CSV
    "The CSV file have the following path~ C:/[csv]"
    $csvName = Read-Host 'enter name of csv file without the type extension (ex. "userlist") '
    $fullcsvname = $csvName + ".csv"
    $csv = import-csv c:\"$fullcsvname" 
    foreach($person in $csv) {
        $manager = $person.manager   #CHANGE COLLUMN HEADER HERE. ex- $person.manager >>> $person.fullName 
        $newStr = nameFlip $manager       # ^ this must match this v
        $person.manager = $newStr    #ALSO CHANGE IT HERE
    }

    Export-Csv -path c:\newCSV -InputObject $csv
}

function nameFlip ($name) {
    # Accepts a name with "last, first" formatting changes it to "first last"
    $namelist = $name -split ','
    $firstname = $namelist[1]
    $lastname = $namelist[0]
    $newname = $firstname + " " + $lastname
    return $newname }

main

object but am having trouble exporting back to a csv file. Simply exporting the object only exports its properties (like length). Appending each item in the csv object is not working either. There has got to be a simple way to do this but I have not been able to find it through troubleshooting or the internet. 

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>