To create a Contact record in Dynamics 365 (D365) using PowerShell, including All Type Attributes.
Step 1. To establish the connection with D365 Run the below code and provide a valid username, 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.
Step 3. Create an Account Record before Updating the Account in the Contact entity or we can use an existing Account here we were Creating an account and using an account in contact.
# Account lookup
$accountId = New-CrmRecord account @{"name"= "Purushotham"}
$accountReference = New-CrmEntityReference -EntityLogicalName account -Id $accountId
# Create contact data
$contact = @{
"firstname" = "Allam" # Single Line Text
"lastname" = "Purushotham" # Single Line Text
"parentcustomerid" = $accountReference # Lookup
"new_boolean" = $true # Boolean
"new_dateofbirth" = Get-Date -Year 2000 -Month 1 -Day 1 # Date Type
"numberofchildren" = 2 # Whole Number
"gendercode" = New-CrmOptionSetValue -Value 1 # Option Set Field
"annualincome" = New-CrmMoney -Value 1000.01 # Currency
}
# Create Contact Record
$contactId = New-CrmRecord -EntityLogicalName contact -Fields $contact
Write-Host "Contact created: $($contact.firstname) $($contact.lastname) $($contact.annualincome)"
Step 4. Select the Code and click on the Run Selection button.
After execution of Code in Windows Power Shell Script.
Step 5. Contact Record Created in D365.