On PowerShell.com, I answered a question from a forum member who was receiving a CSV with the list of user names to be added to a group. If the CSV file had a blank line at the end of it, it would cause problems. Below is my code to read in a CSV file and filter out any blank lines before handing the object to PowerShell for processing.
Import-Csv -Path "F:\Temp\Users.csv" |
ForEach-Object {If($_.Name -ne "") {Add-ADGroupMember -Identity "Test Group" `
-Members $_.Name}}
We first import the CSV file into the PowerShell pipeline. Next we pass the object to a ForEach-Object statement. Each object is tested to see if the name property is empty. If it is, then nothing is done. If it contains data, then the user is added to the group. I could not use Where-Object {$_.Name –ne “”} as my filter because Add-ADGroupMember does not accept input from the PowerShell pipeline for its Member parameter.
Comments