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"
paul_techyAsked:
Who is Participating?
 
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

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

Will.
0
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.

All Courses

From novice to tech pro — start learning today.