I am in need to iterate through a list of emails (that happen to be username in Sitecore CMS) and then remove the users. I would like to have an "if" statement to see if account is present before attempting to remove it.
# list of emails, one per line, ex. "[email protected]"
$removeEmails = Get-Content C:\AccountsToRemove.txt
foreach ($email in $removeEmails) {
$account = "Storefront\$($email)"
if (Test-Account -Identity $account)
{
$email | Remove-User
Write-Output "Removed: '$email'"
}
}
But I get PowerShell complaining as follows:
Test-Account : Cannot bind parameter 'Identity'. Cannot convert value "Storefront\[email protected]" to type "Spe.Commands.Security.AccountIdentity". Error: "The name 'Storefront\[email protected]' is improperly formatted.
The name can only contain the following characters: a-z, 0-9, periods, dashes, underscores, backslashes, and spaces. Parameter name: name"
How do I overcome this issue?
Thank you for your help in advance!!
UPDATE:
I found a thread mentioning that RegEx is too restrictive: Cannot add a User to a Role where the User name contains the "@" sign using PSE
Hopefully issue will be resolved in some next version. Still same issue in Sitecore PowerShell Extensions 6.3.0.17327 Windows PowerShell 5.1 (Sitecore 9.3)
BUT if anyone can think of a workaround... please help!! and no,
Get-User -Filter * | Where-Object {$_.Profile.Email -like "[email protected]"} | Remove-User
is not viable with over million users - Sitecore CM hangs like crazy. This is based of post here: https://munirhassan.com/2021/03/29/remove-sitecore-users-from-the-core-database/
NOTE 1 (sitecore domain)
Was able to add user with email as username after easing restrictions with:
<setting name="AccountNameValidation" value=".+" />
and running the script (user is in "sitecore" domain: "sitecore\[email protected]")
NOTE 2 (Storefront domain with plus)
NOTE 3 (Storefront domain no plus)