I'm facing a strange behavior regarding DateTime conversion when using the Set-Mailbox command to set retention settings on a client, having german culture settings. In my use case, this is done in a .NET exe using the classes from
System.Management.Automation. The issue can be reproduced on a "regular" shell.
After creating an Exchange Online powershell connection, run the following commands for a user, that has RetentionHoldEnabled set to false:
$dtStart = New-Object System.DateTime -ArgumentList 2025,12,29,0,0,0
$dtEnd = New-Object System.DateTime -ArgumentList 2025,12,31,0,0,0
Set-Mailbox -Identity <user> -RetentionHoldEnabled $true -StartDateForRetentionHold $dtStart -EndDateForRetentionHold $dtEnd
When regional settings of the computer are set to "United States", the command runs fine. When set to "Germany", you'll get the following error:
Error on proxy command 'Set-Mailbox -Identity:'...' -RetentionHoldEnabled:$True -StartDateForRetentionHold:'29.12.2025 00:00:00'
-EndDateForRetentionHold:'31.12.2025 00:00:00' -Confirm:$False -Force:$True' to server <...>.<...>.prod.outlook.com: Server version 15.01.1075.0000, Proxy method PSWS:
Cmdlet error with following error message:
System.Management.Automation.ParentContainsErrorRecordException: Cannot process argument transformation on parameter 'StartDateForRetentionHold'.
Cannot convert value "29.12.2025 00:00:00"
to type "System.DateTime". Error: "String was not recognized as a valid DateTime."..
+ CategoryInfo : NotSpecified: (:) [Set-Mailbox], CmdletProxyException
+ FullyQualifiedErrorId : Microsoft.Exchange.Configuration.CmdletProxyException,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
+ PSComputerName : outlook.office365.com
As you can see, the command send to the server converts the DateTimes (dtStart/dtEnd) to string using the german culture. The remote machine then fails to parse the string back to a valid DateTime.
If you try to execute Set-Mailbox and pass the Datetime directly as string in the format
yyyy-mm-dd it fails with the same error:
Set-Mailbox -Identity <Identity> -RetentionHoldEnabled $true -StartDateForRetentionHold "2025-12-29" -EndDateForRetentionHold "2025-12-31"
Error on proxy command 'Set-Mailbox -Identity:'<...>' -RetentionHoldEnabled:$True -StartDateForRetentionHold:'29.12.2025 00:00:00'
-EndDateForRetentionHold:'31.12.2025 00:00:00' -Confirm:$False -Force:$True' to server <...>.<...>.prod.outlook.com: Server version 15.01.1075.0000, Proxy method PSWS:
Cmdlet error with following error message:
System.Management.Automation.ParentContainsErrorRecordException: Cannot process argument transformation on parameter 'StartDateForRetentionHold'.
Cannot convert value "29.12.2025 00:00:00"
to type "System.DateTime". Error: "String was not recognized as a valid DateTime."..
+ CategoryInfo : NotSpecified: (:) [Set-Mailbox], CmdletProxyException
+ FullyQualifiedErrorId : Microsoft.Exchange.Configuration.CmdletProxyException,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
+ PSComputerName : outlook.office365.com
Things get strange when you do that operation in two steps
$dtStart = New-Object System.DateTime -ArgumentList 2025,12,29,0,0,0
$dtEnd = New-Object System.DateTime -ArgumentList 2025,12,31,0,0,0
Set-Mailbox -Identity <user> -RetentionHoldEnabled $true
Set-Mailbox -Identity <user> -StartDateForRetentionHold $dtStart -EndDateForRetentionHold $dtEnd
This works (almost) fine no matter if region is set to United States or Germany. The drawback of this "workaround" is, that sometimes the second "Set-Mailbox" does not recognize, that RetentionHoldEnabled was enabled and then fails with an error message,
stating that RetentionHoldEnabled must be enabled before start- and enddate can be set.
The conversion error also occurs, when I try to set other properties along with start- and enddate (such as RetentionComment).
Am I missing something or am I doing something wrong? (Just to clarify, I've a workaround but the behavior seems very strange to me)
The client computer is btw. Windows 10 Enterprise
PSVersion 5.1.14393.1066