title: Loop Multiple Values In Variable Output To csv Powershell
body: <p>I created a script where a dialogue box pops up and the value you enter goes into foreach. I didn't know how to take a single value created in a running script and process it. It worked, although I knew it wasn't the right way to do it.. Now I've created a loop to prompt again and the goal is to append each value into a csv. My problem is now the original variable value is overwritten by the next value put into the prompt before writing out to a csv. How can I build each entry into the looping dialogue box and create the csv?</p>
  
  <p>You can see in the powershell script that I create $x from the value input to the dialogue box, then I cycle the script into a function that repeats the dialogue prompt. When it does that it overwrites the first value of $x. I'm trying to figure out how to build many values before writing the all to the csv.</p>
  
  <p>This script is to have a user enter a group, check it against Active Directory, and then generate a CSV.</p>
  
  <p>...Update## I was able to resolve it. I'm removing the original test code and putting the final product. The following script creates a form which asks for an object in Active Directory. It checks active directory then outputs to a spreadsheet, and asks again until canceled. Building the variable repeatedly.</p>
  
  <pre><code>function ProcessOut ($x , $group) {
              $result = @()
  
              Foreach ($Line in $x){
                              $GroupName = "domain.local\" + $group
                              $OutList = New-Object System.Object
                              $OutList | Add-Member -type NoteProperty -Name "DisplayPath_GroupName" -value $GroupName
                              $OutList | Add-Member -type NoteProperty -Name "RuleName" -value "AutomaticApproval"
                              $OutList | Add-Member -type NoteProperty -Name "RuleClauses" -value '
                              $result+= $OutList
              }
  
              #Output to csv
              $outputfilepath = 'c:\users\technician\desktop\'
              $outputfilename = $outputfilepath + 'FinalFile.csv'
              $result | export-csv $outputfilename  -Append -encoding unicode -NoTypeInformation
  }
  
  function PromptInput {
              add-Type -AssemblyName System.Windows.Forms
              Add-Type -AssemblyName System.Drawing
  
              $form = New-Object System.Windows.Forms.Form
              $form.Text = 'Group Auto-Approval Setup'
              $form.Size = New-Object System.Drawing.Size(500,230)
              $form.StartPosition = 'CenterScreen'
  
              $OKButton = New-Object System.Windows.Forms.Button
              $OKButton.Location = New-Object System.Drawing.Point(170,100)
              $OKButton.Size = New-Object System.Drawing.Size(75,23)
              $OKButton.Text = 'OK'
              $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
              $form.AcceptButton = $OKButton
              $form.Controls.Add($OKButton)
  
              $CancelButton = New-Object System.Windows.Forms.Button
              $CancelButton.Location = New-Object System.Drawing.Point(260,100)
              $CancelButton.Size = New-Object System.Drawing.Size(75,23)
              $CancelButton.Text = 'Cancel'
              $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  
              $form.CancelButton = $CancelButton    
              $form.Controls.Add($CancelButton)      
  
              $label = New-Object System.Windows.Forms.Label
              $label.Location = New-Object System.Drawing.Point(200,40)
              $label.Size = New-Object System.Drawing.Size(280,20)
              $label.Text = "Enter a group name:"
              $form.Controls.Add($label)
  
              $textBox = New-Object System.Windows.Forms.TextBox
              $textBox.Location = New-Object System.Drawing.Point(100,65)
              $textBox.Size = New-Object System.Drawing.Size(300,120)
              $form.Controls.Add($textBox)
  
              $form.Topmost = $true
  
              $form.Add_Shown({$textBox.Select()})
              $result = $form.ShowDialog()
  
              if ($result -eq 'Cancel'){
                              Exit
              }
  
              if ($result -eq [System.Windows.Forms.DialogResult]::OK){
                              $x = $textBox.Text
              }
  
              return $x
  }
  
  $continue = $true
  while($continue){
              $input = PromptInput
              Add-Type -AssemblyName microsoft.visualbasic
  
              $searcher = [ADSISearcher]"(SAMAccountName=$input)"
              $result = $searcher.FindOne()
  
              if($result){
                              ProcessOut $result $input
                              $additional = [System.Windows.Forms.MessageBox]::Show("Would you like to enter another group?" , "Status" , 4)
                              if ($additional -eq "NO"){
                                              Exit
                              }
              } else{
                              [System.Windows.Forms.MessageBox]::Show("Group name not found - Please enter a valid group name")
              }
  } 
  </code></pre>