run multiple commands Exchange powershell

I have an Exchange powershell file .ps1 named forwardoff.ps1 which has the following commands. I right click on the .ps1 files and run with powershell but it errors out.  When I run manually thru exchange PowerShell each command one at a time they run successfully. What am I doing wrong.? I need to be able to see the result which is why I added the "press any key to continue"

Set-Mailbox user1 -ForwardingAddress $Null;
Set-Mailbox user2  -ForwardingAddress $Null;
Set-Mailbox user3 -ForwardingAddress $Null;
Set-Mailbox user4 -ForwardingAddress $Null;

Read-Host -Prompt "Press Enter to continue"
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Will SzymkowskiSenior Solution ArchitectCommented:
Remove the " ; " from the end of each command. and try again.

Sudeep SharmaTechnical DesignerCommented:
Well there are other ways to do what you wanted to do.

If you have list of Users you wanted to remove the forwarding then you could use the following script.
$Mailboxes= Import-CSV C:\Temp\Users.csv 
ForEach ($Mailbox in $Mailboxes)
{Set-Mailbox $Mailbox.users -ForwardingAddress $Null}

Open in new window

If you wanted to remove the forwarding on all the Users then try the below script:
Get-Mailbox | Where {$_.ForwardingAddress -ne $null} | Set-Mailbox -ForwardingAddress $null -DeliverToMailboxAndForward $false

Open in new window


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
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

From novice to tech pro — start learning today.