1

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]")

Remove user with email as username - sitecore domain

NOTE 2 (Storefront domain with plus)

Remove user with email as username - Storefront domain

NOTE 3 (Storefront domain no plus)

Remove user with email as username -Storefront domain no plus

1
  • Can you add this setting to allow user creation with email? and try example I posted. <setting name="AccountNameValidation" value=".+" /> Jun 24, 2022 at 22:46

1 Answer 1

2

Working example from 9.3

    $removeEmails = @("[email protected]","[email protected]")
    foreach ($email in $removeEmails) {
        $account = "Storefront\$email"
        if (Test-Account -Identity $account)
        {
           $account | Remove-User
           Write-Output "Removed: $account"
        }
        else
        {
           Write-Output "Not found user: $account"
        }
     }
12
  • No, it does not work. Now "Storefront" part is lost.
    – Sasha
    Jun 24, 2022 at 20:45
  • And the issue is that it does not like the "@" in the username (this is being an email and all). I believe this is the issue. Thoughts?
    – Sasha
    Jun 24, 2022 at 20:56
  • what is your sitecore version? Jun 24, 2022 at 21:01
  • 9.3.0 rev. 003498
    – Sasha
    Jun 24, 2022 at 21:05
  • I see that you updated code in your question so updating answer with working example from SC 9.3 Jun 24, 2022 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.