Help running through a CSV line by line (I know part of the command to use)

Hey everyone,

I’m working on going through a list I’ve already pulled and put into a CSV.

I essentially know the command I need to do what I want to do, but I don’t know how to start it so it knows to run through line by line.

The CSV basically looks like the below:

Line Name SID AccessRights
1 SMBox1 S-1-5-21-2081497765-1548102756-51780 FullAccess
2 SMBox1 S-1-5-21-168066109-405644716-6914220 FullAccess
3 SMBox2 S-1-5-21-2081497765-1548102756-13268 FullAccess, DeleteItem
 

Basically, do something with line 1… SMBox, using SID from line 1 and the AccessRights from line 1. Then do something with line 2… SMBox2, using the SID from line 2, and the AccessRights, etc.

All the way down my list.

Code Example:

Remove-MailboxPermission -Identity (SMBOX1) -USER (SIDLine1) -AccessRight (WhateverTheyHave) -Confirm:$false

 

I don't know if it should be some kind of loop. Importing a CSV and telling it to look at the column for the identity variable and the other columns for the other parts, etc.. I don't know if an array of some kind would be better (never used them or set one up).
I just need help going through the list line by line to do something. And this could be used in the future, like creating a bunch of new Distribution Lists with names and emails in the columns, removing users from AD, changing server names, whatever. I just need to know how to go through the file/csv/array.
Thanks

Personally, I would use Import-Csv.

Import-Csv -Path C:\AccessRights.csv | ForEach-Object -Process {
 
    Remove-MailboxPermission -Identity $_.Name -User $_.SID -AccessRight $_.AccessRights -Confirm: $false

}

So then each header of each column would have those corresponding names, correct? Like what I have in the example table?

 

 

Edit:

Gave that a test with some of the actual ones I have. Boy I feel dumb. I didn’t think it would be smart enough to go line by line through an entire CSV without giving it more than a basic loop. I figured I had to specify each column for a variable and have it proceed that way.

 

Thanks!!!

Correct. Each header is a property name.

Another option is to use a standard foreach and define the token as $_ can get confusing if you do any where-object where you would also use $_. Here is an example with splatting:

foreach ($mbx in  (Import-Csv -Path C:\AccessRights.csv)) {
 
    $params = @{
        Identity    = $mbx.Name 
        User        = $mbx.SID 
        AccessRight = $mbx.AccessRights 
        Confirm     = $false
    }

    Remove-MailboxPermission @params
 
}

nice