How to fix @{DisplayName=Firstname Lastname}

Viewed 345

@{DisplayName=Firstname Lastname} needs to be just 'Firstname Lastname' because get-mailbox -identity '@{DisplayName=Firstname Lastname} won't work.

I've tried using the -replace cmdlet to remove text.

$Olduser = Get-MsolUser -all | Where-Object {$_.BlockCredential -eq $True -and $_.isLicensed -eq $false} | Select-Object displayName,userPrincipalName,BlockCredential,isLicensed

$OldUser | fl *

Using the -replace, I expected that the output will be without '@{DisplayName}'


Cannot process argument transformation on parameter 'Identity'. Cannot convert value "@{DisplayName=X X}" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "Cannot conve
rt the "@{DisplayName=X X}" value of type "Deserialized.Selected.Microsoft.Online.Administration.User" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter"."
    + CategoryInfo          : InvalidData: (:) [Get-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Mailbox
    + PSComputerName        : outlook.office365.com
1 Answers

You are passing an object type with properties to a parameter (-Identity) that expects just a value. As a result, PowerShell converts that object into a string, which results in the value @{DisplayName=Firstname Lastname}. If you access the DisplayName property before passing it into the -Identity parameter, your issue will be resolved.

Get-Mailbox -Identity $OldUser.DisplayName
# Or
$OldUser.DisplayName | Get-Mailbox

You can replicate a similar behavior by typing the following at the console:

[string]$OldUser
Related