Very often people ask our Help Desk to set a user mailbox to forward it to another user mailbox. I want to do it through Powershell which will be faster.
I managed to do it but i want to add a code that checks the user mailbox that it’s not already configured to forward the emails to another user mailbox and if so to get the name of that mailbox user.
I can’t extract the name from $currentuser in order to write it in the console.
this is the script:
$userfrom = Read-Host "Please enter username to forward emails from"
$userto = Read-Host "Please enter username to forward emails to"
$currentuser = Get-Mailbox $userfrom | ft ForwardingAddress
If ($currentuser) {
Write-Host -ForegroundColor Red $userfrom, " Mailbox is already set to forward email to " , $currentuser
$Answer = Read-Host "`n1. Change anyway `n2. Leave as it is"
switch ($Answer) {
"1" {Set-Mailbox $userfrom -ForwardingAddress $userto -DeliverToMailboxAndForward $True
Write-Host -ForegroundColor Red "Changes to", $userfrom, "Were made!"
}
"2" {Write-Host -ForegroundColor Red "No changes were done!"}
}
}
and this is what i get:
Please enter username to forward emails from: First-user
Please enter username to forward emails to: Second-user
First-user Mailbox is already set to forward email to Microsoft.PowerShell.Commands.Internal.Format
.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.C
ommands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData M
icrosoft.PowerShell.Commands.Internal.Format.FormatEndData
- Change anyway
- Leave as it is:
Thanks in advance.