To update a contact record in Dynamics 365 (D365) using PowerShell, you can utilize the Update-CrmRecord cmdlet provided by Microsoft.Xrm.Data.PowerShell module. Below is an example script that demonstrates how to update a contact record with various types of attributes.
Step 1. For established the connection with D365 Run the below code and provide a valid user name, password, and Url.
Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$Username = "username.onmicrosoft.com"
$Password = "password"
$pwd = ConvertTo-SecureString $Password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($Username, $pwd)
$CRMConnection = Connect-CrmOnline -Credential $credentials -ServerUrl "https://allamurl.crm8.dynamics.com/" -ForceOAuth
Step 2. Check the connection $CRMConnection.
Sample Code
# Account lookup
$accountId = New-CrmRecord account @{"name"= "A Anusha"}
$accountReference = New-CrmEntityReference -EntityLogicalName account -Id $accountId
# Create contact data
$contact = @{
"firstname" = "A" # Single Line Text
"lastname" = "Anusha" # Single Line Text
"parentcustomerid"= $accountReference # Lookup
"new_boolean" = $false # Boolean
"new_dateofbirth" = Get-Date -Year 2000 -Month 1 -Day 1 # Date Type
"numberofchildren"= 2 # Whole number
"gendercode" = New-CrmOptionSetValue -Value 2 # Option Set Field
"annualincome" = New-CrmMoney -Value 2000.01 # Currency
}
$contactRecord = Set-CrmRecord -conn $conn -EntityLogicalName contact -Id f19e973f-6fe3-ee11-904c-6045bdcf0e8a -Fields $contact
Write-Host "Contact Updated: $($contact.firstname) $($contact.lastname) $($contact.annualincome)"
Step 3. Select the Code and click on the Run Selection button.
Step 4. After executing the Contact Update code in the Windows PowerShell script.
Step 5. Before Updating the Contact Record.
Step 6. After Updated the above Contact Record.