CRUD Operations in Dynamics 365 through PowerShell

Connection to Dynamics 365 using the provided information.

Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

Install Windows Powershell

$Username = "apurushotham***@Trailcrm.com"
$Password = "al****pu"
$pwd = ConvertTo-SecureString $Password -AsPlainText -Force 
$credentials = New-Object System.Management.Automation.PSCredential($Username, $pwd)
$conn = Connect-CrmOnline -Credential $credentials -ServerUrl "https://apurushtrailcrm****.crm.dynamics.com/" -ForceOAuth

Execution policy

Verify the connection status with the $conn object. If IsReady is True, the connection has been successfully established.

Create a new Account record in D365 using Powershell.

Syntax

New-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Fields] <Hashtable> [<CommonParameters>]

Example

$parentId = New-CrmRecord account @{"name"="parent account name"}
$parentReference = New-CrmEntityReference -EntityLogicalName account -Id $parentId
New-CrmRecord -conn $conn -EntityLogicalName account -Fields @{"name"="Allam Purushotham";"industrycode"=New-CrmOptionSetValue -Value 1;"parentaccountid"=$parentReference;"new_boolean"= $true;"new_dateofbith"=[datetime]"2000-01-01"} 

Power Apps

Update an Account record in D365 using Powershell.

Syntax

Set-CrmRecord [-conn <CrmServiceClient>] [-CrmRecord] <PSObject> [-Upsert <SwitchParameter>] [-PrimaryKeyField <String>] [<CommonParameters>]
Set-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Id] <Guid> [-Fields] <Hashtable> [-Upsert <SwitchParameter>] [-PrimaryKeyField <String>] [<CommonParameters>]

Example

Set-CrmRecord -conn $conn -EntityLogicalName account -Id 427e04ce-45b0-ee11-a568-000d3aa3576a -Fields @{"name"="Allam Purushotham name";"industrycode"=New-CrmOptionSetValue -Value 1;""creditonhold"= $true;"new_datetimeapy"=[datetime]"2023-01-01T12:00:00";"parentaccountid"=$parentReference}

 

Power Apps account

Get Account record in D365 using Powershell

Syntax

Get-CrmRecords [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [[-FilterAttribute] <String>] [[-FilterOperator] <String>] [[-FilterValue] <String>] [[-Fields] <String[]>] [[-AllRows] <SwitchParameter>] [[-TopCount] <Int32>] [<CommonParameters>]

Example Retrieve Multiple

$acc = Get-CrmRecords -conn $conn -EntityLogicalName account -FilterAttribute name -FilterOperator "eq" -FilterValue "account name" -Fields name,accountnumber

Example RetrieveSingle Record

$onerecord = $acc.CrmRecords[0]

Example Get Account records in D365 using Powershell by View Name.

Get-CrmRecordsByViewName -conn $conn -ViewName "Active Accounts"

Delete Account record in D36 using powershell

Syntax

Remove-CrmRecord [-conn <CrmServiceClient>] [-CrmRecord] <PSObject> [<CommonParameters>]
Remove-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Id] <Guid> [<CommonParameters>]

Example

Remove-CrmRecord -conn $conn -EntityLogicalName account -Id 3f239485-6d37-****-****-6045bd006e13


Similar Articles