Reputation: 275
I'm trying to export forward list from Exchange 365 and remove "smtp:" string from output.
Here is PS code for Exchange 365 forwards list:
$forwards = Get-Mailbox –ResultSize Unlimited |
where {($_.ForwardingSmtpAddress –ne $null)} |
select identity, *forward*
$result = $forwards -replace "smtp:",""
$result
My $forwards
value has format:
PS> $forwards Identity DeliverToMailboxAndForward ForwardingAddress ForwardingSmtpAddress -------- -------------------------- ----------------- --------------------- name.surname1 True smtp:[email protected] name.surname2 True smtp:[email protected] name.surname3 True smtp:[email protected]
I managed to remove that smtp:
text with command:
$result = $forwards -replace "smtp:",""
But I get such ugly output for that variable:
PS> $result @{Identity=name.surname1; DeliverToMailboxAndForward=True; ForwardingAddress=; [email protected]} @{Identity=name.surname2; DeliverToMailboxAndForward=True; ForwardingAddress=; [email protected]} @{Identity=name.surname3; DeliverToMailboxAndForward=True; ForwardingAddress=; [email protected]}
How to get rid of that @{}
and present it right way as it was in $forwards
variable? Maybe there is better way to remove smtp:
from first value $forwards
?
Upvotes: 0
Views: 235
Reputation: 891
You should also be able to use calculated properties:
$forwards = Get-Mailbox –resultSize unlimited |
where { $_.ForwardingSmtpAddress –ne $null } |
select @{Name="Identity";Expression={$_.Identity}},
DeliverToMailboxAndForward, ForwardingAddress,
@{Name="ForwardingSmtpAddress";Expression={$_.ForwardingSmtpAddress -replace "smtp:",""}}
I don't have the means to test this right now, though.
Upvotes: 4
Reputation: 2208
What would be the result when you use this snippet?
$forwards = Get-Mailbox –resultSize unlimited |
where {$_.ForwardingSmtpAddress –ne $null} |
select Identity, DeliverToMailboxAndForward, ForwardingAddress,
@{Name='ForwardingSmtpAddress';Expression={$_.ForwardingSmtpAddress.ToString().Replace('smtp:', $null)}}
If you don't wan't the @{}
maybe you could convert or export it to a CSV format.
Export-Csv -Delimiter ';'
ConvertTo-Csv -Delimiter ';'
Upvotes: 0
Reputation: 22861
Instead of creating a new variable it might be easier to modify the existing objects in your array. You need to do the replacement on each ForwardingSmtpAddress
property:
$forwards | Foreach-Object {
$_.ForwardingSmtpAddress = $_.ForwardingSmtpAddress -replace "smtp:", ""
}
After this, just echoing $forwards
to the screen should show you the updated values.
Upvotes: 3