title: Importing CSV splits certain lines
body: <p>I have a fairly simple script that needs to check around 20,000 AD Groups for their membership count.  That all works fine, I can take the list of groups run it through the script and for the most entries it works fine.  However I was getting some errors that I couldn't figure out and hopefully someone here can point me in the right direction.</p>
  <p>I am using the DN of the object to query AD and for around 10% it fails, but when I copy the DN from the file, paste it into a command window and run the command manually it works fine.  Some more checking and it seems that when I read an offending line into my variable there is a line break in the middle for some reason.</p>
  <p>When looking at the value of the variable I get the following:</p>
  <p>Working Example - &quot;CN=ABC, OU=Location, OU=Distribution Lists, DC=Domain, DC=COM&quot;</p>
  <p>Error Example - &quot;CN=ABC, OU=Location, OU=Distribution</p>
  <p>Lists, DC=Domain, DC=COM&quot;</p>
  <p>It seems to insert a return in-between Distribution and Lists on certain entries in the file.  I have tried deleting the character in-between and replacing it with a space but I get the same result.</p>
  <p>Could it be the length?  I am still looking for a common factor but any suggestions would be great.</p>
  <p>Thanks</p>
  <p>Updated with requested content.</p>
  <pre><code>$Groups = Import-Csv C:\Temp\DLName.csv
  write-host ($Groups).Count
  $i=1
  
  foreach ($Group in $Groups)
  {
  $GroupInfo = Get-ADGroupMembersRecursive -Groups $Group.Name
  $MembersCount = ($GroupInfo | Measure-Object).Count
  $MembersList = $GroupInfo | Select Name -ExcludeProperty Name
  $FriendlyName = Get-ADGroup -Identity $Group.Name
      
  $Export = $FriendlyName.Name + &quot;, &quot; + $MembersCount 
  $Export | Out-File C:\Temp\DLMembers.csv -Append
  
  Write-host $FriendlyName &quot;,&quot; $MembersCount
  
  $i
  $i++
  }
  </code></pre>
  <p>Entry 1 and 3 work 2 doesn't, but the formatting here seems to have wrapped the entries.</p>
  <p>Name</p>
  <p>&quot;CN=Company - DL Name1,OU=Country1 Distribution Lists,OU=Europe,OU=Acc,DC=Domain,DC=Domain,DC=com&quot;
  &quot;CN=Company - DL Name2,OU=Country2 Distribution Lists,OU=Europe,OU=Acc,DC=Domain,DC=Domain,DC=com&quot;
  &quot;CN=Company - DL Name3,OU=Country3 Distribution Lists,OU=America,OU=Acc,DC=Domain,DC=Domain,DC=com&quot;</p>
  <p>Top pic is the failure second pic works.</p>
  <p><a href="https://i.stack.imgur.com/Ewpcn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ewpcn.png" alt="Not Working" /></a></p>
  <p><a href="https://i.stack.imgur.com/ifGue.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ifGue.png" alt="Working" /></a></p>
  <p>List Creation:</p>
  <pre><code>$SearchScope = &quot;OU=OUName,DC=Domain,DC=Domain,DC=com&quot;
  $SearchFilter = {GroupCategory -eq 'Distribution'}
  $Groups = Get-ADGroup -SearchBase $SearchScope -Filter 
  $SearchFilter | Sort-Object Name
  
  foreach ($Group in $Groups)
  {
  
  $Group.DistinguishedName | Select Name -ExpandProperty Name
  $Group.DistinguishedName | Out-File C:\Temp\DLName.csv -Append
  
  }
  </code></pre>