Get-mailbox ForwardingAddress

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

  1. Change anyway
  2. Leave as it is:

Thanks in advance.

Hi,
Maybe something like this :slight_smile:

$userfrom = Read-Host "Please enter username to forward emails from"
$userto = Read-Host "Please enter username to forward emails to"
$currentuser = Get-Mailbox $userfrom | select ForwardingAddress
$Usertoidentity = Get-Mailbox $userto | select Identity

    If ([string]::IsNullOrEmpty($currentuser.ForwardingAddress)){
            write-host -ForegroundColor green "$userfrom has an empty ForwardingAddress"
            $Answer = Read-Host "`n1. Set ForwardingAddress to: $userto `n2. Leave as it is `nChoose"
        switch ($Answer) {
            "1" {Set-Mailbox $userfrom -ForwardingAddress $userto -DeliverToMailboxAndForward $True
                 Write-Host -ForegroundColor Blue "ForwardingAddress is now: $userto !"
                
                }
            "2" {Write-Host -ForegroundColor Red "No changes were done!"
                
                }
            
            }
    }

    If ($currentuser.ForwardingAddress -eq $Usertoidentity.Identity) {
        Write-Host -ForegroundColor Red $userfrom, "mailbox is already set to forward email to", $userto
    }

    ElseIf ($currentuser.ForwardingAddress){
            
            $Answer = Read-Host "`n1. Change ForwardingAddress to $userto `n2. Leave as it is `nChoose"
        switch ($Answer) {
            "1" {set-Mailbox $userfrom -ForwardingAddress $userto -DeliverToMailboxAndForward $True
                 Write-Host -ForegroundColor Blue "Changes to", $userfrom, "Were made! "
                
                }
            "2" {Write-Host -ForegroundColor Red "No changes were done!"
                
                }
            
            }
    }

Hi DejvDzi,

Thanks for your reply. Your script works but i guess it was my bad that i didn’t explain myself enough. I wanted to check if the mailbox is already set to forward email and to write to which user it’s already set to.

I took some ideas from your script and i managed to extract the username actually very easy.

The
$currentuser.ForwardingAddress.Name gave what i needed.

Thanks