I agree with @iRon, Get-Content
cmdlet returns an array of strings for each line.
But -AdvancedRule
parameter of the New-DLPComplianceRule
cmdlet expects a single string as input.
When I ran the same code in my environment, I got same error saying Connot convert value to type System.String
:
Connect-IPPSSession
$data = Get-Content -Path "C:\Data\Sensitive Type.txt" -ReadCount 0
New-DLPComplianceRule -Name "Contoso Rule 1" -Policy "Contoso Policy 1" -AdvancedRule $data -NotifyUer
Response:

To resolve the error, you need to add Out-String
at the end that converts the array of strings returned by the Get-Content cmdlet to a single string.
When I added Out-String
at the end, I got the response successfully like below:
$data = Get-Content -Path "C:\Data\Sensitive Type.txt" -ReadCount 0 | Out-String
New-DLPComplianceRule -Name "Contoso Rule 1" -Policy "Contoso Policy 1" -AdvancedRule $data -NotifyUser user@xxxxxxxxxxx.onmicrosoft.com
Response:

To confirm that, I checked the same in Microsoft Purview Portal where Data loss prevention compliance rule named Contoso Rule 1
created successfully:

If it's working in your colleagues PC without adding Out-String
, verify whether you are using different version of PowerShell compared to them by running below command:
$PSVersionTable.PSVersion
