How do I add multiple users to a Powershell command so that they have access to a calendar on a mailbox?

I have the command I need to add one user to my calendar -
Add-MailboxFolderPermission John:\calendar  -AccessRight PublishingEditor -User Suzan 

Open in new window


But what if, in addition to Suzan, I want Patrick, Adrian and Paul?

thanks
LVL 4
fuzzyfreakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Justin YeungSenior Systems EngineerCommented:
[array]$Users = @("Suzan","Patrick","Adrian","Paul")
foreach ($User in $Users)
{
Add-MailboxFolderPermission John:\calendar  -AccessRight PublishingEditor -User $User
}
0
fuzzyfreakAuthor Commented:
Justin, can you please put the code tags around this because the spaces are confusing me - I tried the code without the spaces and it doesn't like various parts such as 'foreach' and 'in' and '(' and ')'

thanks
0
Justin YeungSenior Systems EngineerCommented:
[array]$Users = @("Suzan","Patrick","Adrian","Paul")
 foreach ($User in $Users)
 {
 Add-MailboxFolderPermission John:\calendar  -AccessRight PublishingEditor -User $User
 } 

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

fuzzyfreakAuthor Commented:
Thanks I'll give this a go once back in the office.
0
fuzzyfreakAuthor Commented:
Justin, thank you very much, this worked perfectly!

I now wish to apply this to multiple mailboxes, I tried adjusting that script to simply add the additional one underneath but this did not work, see below for example -

[array]$Users = @("Suzan","Patrick","Adrian","Paul")
 foreach ($User in $Users)
 { Add-MailboxFolderPermission John:\calendar  -AccessRight PublishingEditor -User $User }
 { Add-MailboxFolderPermission alan:\calendar  -AccessRight PublishingEditor -User $User } 

Open in new window

0
Justin YeungSenior Systems EngineerCommented:
foreach command needs to run within the script block {}

example
 
[array]$abc = @("1","2","3")
foreach ($abc in $abc}
{
write-host "1"
write-host "2"
}

write-host 3

the result will be

1
2
1
2
1
2
3

since we have 3 object in the array, so command 1 (write-host "1") and command 2 (write-host "2") will run 3 times
and command 3 will execute after the foreach command completed, so write-host "3" with result 3 will appear at this end.

so in your scenario, the command should be like below.

[array]$Users = @("Suzan","Patrick","Adrian","Paul")
 foreach ($User in $Users)
 {
 Add-MailboxFolderPermission John:\calendar  -AccessRight PublishingEditor -User $User 
 Add-MailboxFolderPermission alan:\calendar  -AccessRight PublishingEditor -User $User 
} 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fuzzyfreakAuthor Commented:
This is perfect, worked like a dream.

thank you very much!
0
fuzzyfreakAuthor Commented:
Excellent solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.