Function return values to CSV

Welcome Forums General PowerShell Q&A Function return values to CSV

  • This topic has 3 replies, 2 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #233704
      Participant
      Topics: 5
      Replies: 7
      Points: 47
      Rank: Member

      Hi

      How do I get the return values from a function to a csv-file? Next code indicates what I want to do:

      Function New-Array
      {
      $array = @()
      
      for($i=0;$i -lt 50)
      {
      $array += some_value
      }
      
      return $array
      }
      
      New-Array | Export-Csv -Path some_path

      For the moment I don’t get the right values, but the number 10 on each row. The number of rows is correct.

    • #233710
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      The proper way to create such a function would be this:

      Function New-Array {
          $array = for ($i = 0; $i -lt 50; $i++) {
              $i
          }
          return $array
      }

      or even simpler like this:

      Function New-Array {
          for ($i = 0; $i -lt 50; $i++) {
              $i
          }
      }

      But actually you don’t need a function to create an array of increasing numbers. This would work as well:

      1..50
    • #234073
      Participant
      Topics: 5
      Replies: 7
      Points: 47
      Rank: Member

      Olaf, the real function creates passwords and set it to the users. I need to return the array to a csv for distributing the password to the new users.

      I have adopted my function to your first example as suggested:

      $array = for ….

      Still I get the next return instead of the passwords:

      #TYPE System.String
      “Length”
      “10”
      “10”
      “10”
      “10”
      “10”
      “10”
    • #234079
      Participant
      Topics: 5
      Replies: 7
      Points: 47
      Rank: Member

      It’s solved. At the top of the function, I define my parameters and there stood the following:

      [Outputtype([int])

      I changed it to:

      [Outputtype([System.Object[]])]

      Now it works.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.