1

I have a CSV file like below

Data,Name,Age
Test,T1,22
Test2,T2,23
Test3,T3,24

I want to do some processing like this

Foreach(header in CSvFile.Headers)
{
    //Loop through the column data

}

EDIT : Displaying what I am looking for in real code.

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
    $SiteUrl = "http://www.dev.com"
    $Web = Get-SPWeb $SiteUrl

    $description = "Group with contribute access."
    $permissionLevel = "Contribute"

    foreach($groupName in **CSV.Headers**)
    {
        $web.SiteGroups.Add($groupName, $web.SiteUsers["Test\data"], $web.SiteUsers["Test\data"], $description)  
        $group = $web.SiteGroups[$groupName]  
        $loc = Get-Location     
        $Users = "Test\data"    
        $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)  
        $roleDefinition = $web.Site.RootWeb.RoleDefinitions[$permissionLevel]  
        $roleAssignment.RoleDefinitionBindings.Add($roleDefinition)  
        $web.RoleAssignments.Add($roleAssignment)  
        $web.Update() 
        foreach ($User in **groupName.columnData**) { 
            $Web.EnsureUser($User)  
            Set-SPUser -Identity $User -Web $SiteUrl -Group $group  
        } 

    }

    $Web.Dispose();

Can some one please tell me how to achieve this in powershell.

Thanks

sp9
  • 755
  • 3
  • 11
  • 22

1 Answers1

2

How about something like this:

$csvPath = "C:\temp\temp.csv"
$csv = Import-CSV $csvPath
$csvHeaders = ($csv | Get-Member -MemberType NoteProperty).name
foreach($header in $csvHeaders) {
...etc...
Brian Reynolds
  • 402
  • 2
  • 5
  • Thank you so much, this helped me solve an issue with a CSV import I was stuck at for a week! If I could mark other people's questions as solved I would mark this one for sure. – Tanaka Saito Apr 21 '20 at 20:14