1

I have a csv file like this:

samaccountname,enabled
user1,false
user2,false

I want to take each entry in the samaccaountname column and add this exact text:

C:\Users\

then I need to create a new csv file that only contains a single column with the new data like this:

header
C:\Users\User1
C:\Users\User2
C:\Users\User3

what is the best way to accomplish this?

4
  • What have you tried, and how has what you've tried failed? Ideally, you should provide a Minimal, Complete, and Verifiable Example of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. SO is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See How to Ask a Good Question. Commented Jun 8, 2017 at 19:03
  • I've been playing with using this: Import-Csv -Delim ';' disabled.csv | Foreach {$_.samaccountname = "prepend\$($_.samaccountname)";$_} | Export-Csv dispaths.csv -Delim ';' -NoTypeInformation but its telling me it can't find the property im specifying. I thought for sure I was barking up the wrong tree... Commented Jun 8, 2017 at 19:10
  • Read in file to a powershell object, loop through each instance in the CSV and change the property value and then rewrite it back to the file. Commented Jun 8, 2017 at 19:13
  • You should not put code into comments; edit your question to include the new information. Commented Jun 8, 2017 at 19:20

2 Answers 2

1

Another way to skin this cat is to use the custom property creation via Select-Object:

import-csv $yourcsv | Select-Object *,@{Name="fullpath";Expression={"C:\users\$($_.path)"}} | export-csv -notypeinformation $newcsv

this time, tested and does work.

Edit: and if you don't want the original property, just change Select object to select the properties you do want with the custom property included.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for this. however, i keep getting "The property 'samaccountname' cannot be found on this object." when I run it. I am certain cell A1 contains exactly this text: samaccountname. what am I missing here?
this was supposed to be a sample to get you in the right direction. assign 'gc $sourcefile' to a variable, look at the property names it imports and then replace 'samaccountname' with whatever property it is tha tyou want to append "C:\users" to
@soltkr gc is Get-Content which won't have that property, using Import-CSV would get rid of that error.
@thepip3r sorry, I should have been more clear. I didnt just copy and past your example into the powershell. I altered it to fit my csv filename and the column heading I want to append is samaccountname
@benh making that change quited the errors and created the export .csv file. however it was empty. i changed the export-csv at the end of the code to out-file and tried again but I got the same thing an empty csv file...
|
0

Import the csv, loop through the entries of that csv, for each of them create an object with the property name you want. Then export

$DataToExport = Import-CSV $sourcefile | ForEach-Object {
    [pscustomobject]@{
        "Header" = "C:\Users\$($_.samaccountname)"
    }
} 
$DataToExport | Export-CSV $newfile -NoTypeInformation

1 Comment

There was an erroneous trailing " at the pscustomobject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.