0

I am trying to do a simply read host and export the user input to a csv file.

$List = Read-Host -Prompt 'Enter List of files (exclude file extention)'|
Export-Csv "c:\user.csv" -NoTypeInformation

All I get in my file is:

"length"
"42"

I think I need to declare a custom object but what I want is:

User inputs:

filename1
filename2

export-csv C:\list.csv should have:

filename1
filename2

Thanks!

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
Harry123
  • 49
  • 7
  • 2
    Your expected output looks a lot more like a text file rather than a csv. – Santiago Squarzon Jun 11 '21 at 03:39
  • How should a user enter different filenames? Delimited by what? (a comma, a semi-colon, spaces? What is your intended use? Should the entered filenames exist somewhere? What if a user enters an empty string or something like "blah blah blah". Is that OK with you? – Theo Jun 11 '21 at 07:45

1 Answers1

1

You mentioned using a PSCustomObject, so why not use it?

$R_Input = Read-Host -Prompt "Exclude extension! `nEnter File Name" 
$R_Input = $R_Input -split ","

    [PSCustomObject]@{

        "First File Name"  = $R_Input[0]
        "Second File Name" = $R_Input[1]

    } | Export-Csv -Path "c:\user.csv" -NoTypeInformation -Force -Append

<#Input

Exclude extension! 
Enter File Name: Path Number One, Path Number two

#>


<#OutPut

PS C:\WINDOWS\system32> Import-Csv c:\user.csv

First File Name Second File Name
--------------- ----------------
Name Number One  Name Number two

#>

EDIT:

If you dont care about the file names being in a same column, you can use a for loop to iterate though all input (seperated by a comma), and passed into a PSCustomObject. Now it shouldn't matter how much names they input.

$R_Input = Read-Host -Prompt "Exclude extension! `nEnter File Name" 
$R_Input = $R_Input.Trim() -split ","

    for ($i=0; $i -lt $R_Input.Count; $i++) {
        [PSCustomObject]@{

            "File Name"  = $R_Input[$i]

        } | Export-Csv -Path "c:\user.csv" -NoTypeInformation -Force -Append
    } 
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • THe problem is, i'm not sure how many lines the user will input. – Harry123 Jun 11 '21 at 04:20
  • I'm getting the empty pipe element. – Harry123 Jun 11 '21 at 04:40
  • Also, pardon the typos. I'm on my phone lol – Abraham Zinala Jun 11 '21 at 04:40
  • Can you elaborate on the error you're getting? What do you mean? – Abraham Zinala Jun 11 '21 at 04:40
  • An empty pipe element is not allowed. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : EmptyPipeElement – Harry123 Jun 11 '21 at 04:45
  • What did you type in? I'd remove the csv file first as well, or create a new one with a different name. It's will only export whatever you type in. Can you open a new instance of whatever ide you're using, or console, and copy & paste the code , then run it. It works just fine for me. – Abraham Zinala Jun 11 '21 at 04:51
  • 1
    His code is fine, you're copy pasting it wrong. The only way I can imagine you're getting that error is if you do `| Export-Csv ....` without passing any object to it. @Harry123 – Santiago Squarzon Jun 11 '21 at 04:51
  • 1
    @Santi, thx for the reconfirmation! Had me thinking I typed something wrong lol – Abraham Zinala Jun 11 '21 at 04:53
  • Thanks, yeah i copied it wrong. However, when I type in copy and paste a list of files like this: file1 filetest fileold Filetest2 I get this inside the csv file: #TYPE System.String "Length" "3" – Harry123 Jun 11 '21 at 12:28
  • @Harry123, you're not comma `(,)` sperating the file names. – Abraham Zinala Jun 11 '21 at 12:37
  • So when it asks you for input: FileOne, File Two, File Three, etc. All sperated by a comma. This way it doesn't matter if there's a *space* in the file name. – Abraham Zinala Jun 11 '21 at 12:38
  • dang, i think the user inputs it as a single column list. – Harry123 Jun 11 '21 at 14:59
  • If the file names don't have a space, you can split it by a space instead of a comma. – Abraham Zinala Jun 11 '21 at 15:14
  • thanks. the csv still shows: #TYPE System.String "Length" "3" – Harry123 Jun 11 '21 at 15:29
  • I honestly have no clue what you're doing, you're just saying what it's outputting. How is it you're outputting? What are you inputting? A list? Are they all sperated by a space? Do the file names themselves have spaces in them? Did you make any changes to the code? Are you a comma delimeter, a space? – Abraham Zinala Jun 11 '21 at 15:56
  • 1
    sorry i got it to work. @abraham, i really appreciate it!!!!!! – Harry123 Jun 11 '21 at 16:45