Deleting User Profiles Using PowerShell

There are many reasons why we would want to use a PowerShell shell script to delete user profiles.

  1. We don’t want to use or trust the My Site Cleanup job.
  2. We have a batch of stub user profiles that need to be deleted.
  3. We just want to delete some user profiles for testing purposes.
  4. We don’t want to wait 30 days before the User Profile is deleted by the My Site Cleanup timer job(applies only to SharePoint 2016)

NOTE: Deleting user profiles with this script will delete just the user profile.  This will not delete their My Site or send out that notification like the My Site cleanup job. WARNING: This script will delete user profiles specified in the file. The only other means to recover the user profile is to restore the Profile DB from backup.

The List

There are a few ways that we can gather a list of user profiles. We can query the profile database, run GetNonImportedObjects, or we already have a list that we want to delete.

  1. Profile DB query – Select NtName from UserProfile_Full with(nolock) where bdeleted=1
  2. PowerShell:


$upa = Get-spserviceapplication | ?{$_.typename -match "profile"}
Set-SPProfileServiceApplication $upa -GetNonImportedObjects $true | out-file c:\temp\NonImportedProfiles.txt

We just want the list to be a text file with the a NTName on each line.

Parameters


We will just need to modify the top 3 lines to adjust it for our farm. The first is $siteurl. This should be URL that is consuming from the User Profile Service Application. The $file is the location and name of the input file we created above. The $log is for the location of the log file(Yes, there is logging with my PowerShell scripts). Just make sure the directory is created.

One gotcha is to make sure the account running this script has permissions to the User Profile service application. This can be accomplished in Central Admin with adding the account into Permissions with Full Control.

Link to the Script

SharePoint 2016 MIM and SAML/FBA User Profiles

There is not much out there about MIM and SharePoint 2016 with FBA or SAML user profiles.  Using Active Directory Import is still the same as it was in 2013.   Below we will be going through how to set this up for SAML(ADFS) and MIM.
This guide assumes the environment has been setup from these steps at https://technet.microsoft.com/en-us/library/mt637055(v=office.16).aspx

Why should we do this?

The account name for user profiles are important and what is used to get the profile. Windows Authentication user profiles are just “domain\username” for the account name. When SAML or FBA users invoke the user profiles, the profile service will search for the complete claim(example: “i:05.t|adfs30|User1”). If the user profile is not there, a stub profile will be created. This will give the user two profiles. The main one with SAML/FBA will NOT be sync’d since we would need configure MIM know the claim prefix.

 

1.       Open MIM client then Metaverse Designer.  Select “person” then “Add Attribute”.


New Attribute button.


Type in “SPS-ClaimID” for Attribute Name


Click OK

2. Open the ADMA then Configure Attribute Flow. Select user/person


 

a.       Map your Claim identifier for your SAML users to SPS-ClaimID.  The direction should be Import and Direct.


b.       Click “New”.


Find SPS-ClaimProviderType and SPS-ClaimProviderID in MetaVerse object type.  Below is PowerShell to find your SAML Identity Claim.

$tips = Get-SPTrustedIdentityTokenIssuer

foreach($tip in $tips)

{

$name = $tip.Name

$claims = $tip.ClaimTypeInformation


foreach($claim in $claims)

{


If($claim.IsIdentityClaim -eq $true)

{


Write-Host -ForegroundColor DarkGreen “The Identity Claim is listed below for $name“


$claim } }}

 


Select “Advanced” for “Mapping type” and “Import” for Direction.  Click New. 

The constant value SPS-ClaimProviderID will be the name of your SAML provider when you created it. 

SPS-ClaimProviderType will be “Trusted” if you’re using a Trusted provider.


3.       Open SharePoint MA(SPMA). Click on Select Attributes.  Check the box for SPS-ClaimID.

4.      Configure Attribute Flow. Select user/person.

a.       Map SPS-ClaimID to SPS-ClaimID.  The direction will be export and Direct.  Click new. 

I’m using UPN for my environment.  This will be the identifier claim which could be email or samaccountname(see above for PowerShell).


5.       Run a full sync

SharePoint 2013/2016 – Migrate from Windows claims to ADFS

Many farms are moving from Windows Authentication(NTLM or Kerberos) to SAML. This migration and change requires a lot of planning. This guide is only to give a frame work of migrating some of the simpler farms. This guide also assumes that the ADFS server is already setup and just needs to be configured.

The difference of this guide and other guides are two things. We will be using the -UseDefaultConfiguration when created the trusted identity token issuer. This will give us an OOB claims provider instead of having your farm fending for itself with the people picker/claim provider. The next difference is the user and group migrations. We will be using Convert-SPWebapplication instead of developing a script to use Move-SPUser. More details later in the post about both of these.

Requirements:

  1. The SharePoint farm has to be at June 2014 CU (or higher) for SharePoint 2013(15.0.4623.1001).
  2. The web applications must be Windows Claims. SAML is not supported on classic.
  3. ADFS must be backed with the same Active Directory used in Windows Claims.

Steps to be completed:

  1. Configure ADFS
  2. Creating Trusted Identity Provider
  3. Migrate Users and Groups

Config ADFS

As stated above we assume the ADFS server is setup and connected to AD and ready to configure the Relying Party Trusts.

  1. In AD FS, right click Relying Party Trusts and select Add Relying Party Trust.


  2. This will bring up Add Relying Party Trust Wizard. Click Start


  3. Click Enter data about the relying party manually, Next.


  4. Type in a display name and description(optional). Click Next.


  5. We want to select AD FS 1.0 and 1.1 profile since SharePoint does not support anything but SAML 1.1.


  6. We will need to enter in the web application with /_trust. This will need to be HTTPS.


  7. We will need to add in the realm for an identifier. I entered in urn:sp13:contoso. We will be using this on the SharePoint side.


  8. I’m not using Multi-factor so just clicking Next


  9. We’re permitting everybody through. Click Next


  10. Click Next


  11. Click Close and leave the box checked. We will be mapping claims.


  12. In the Issuance Transform Rules, click Add Rule…


  13. Leave the drop down value at Send LDAP Attributes as Claims. Click Next


  14. Give the claim rule a name. Select Active Directory from the dropdown.


  15. We will only be mapping the required fields here. The first one we will map is your identifier claim. This is the claim that a user needs to have in AD to sign in. The only choices Account-Name, Email or USER-PRINCIPAL-NAME. Adjust your guide as needed. I will be using Email. The only other Claim is for security groups.


LDAP Attribute Outgoing Claim Type
User-Principal-Name UPN
E-Mail-Address E-Mail Address
SAM-Account-Name Windows account name
Token-Groups as SIDs Group SID
  1. We will need the Token-sign certificate from ADFS. Click on Certificates, then click on the token-signing certificate, then View Certificate


  2. Click on Details tab and Copy to File…


  3. DER .CER is the file format we want. Click Next


  4. Give the file a name and location. Click Next


  5. Click Finish. Copy the certificate to the SharePoint server.


Creating Trusted Identity Provider

  1. On the SharePoint server, open PowerShell ISE. We will be loading the SharePoint module using Add-PSSnapin *sharepoint*


  2. This is to set the path of the token signing certificate we got from ADFS
$adfscertPath = “c:\adfstoken.cer” 
$adfscert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($adfsCertPath)

3. We want SharePoint to trust the certificate.

New-SPTrustedRootAuthority -Name “ADFS Token Signing Cert” -Certificate $adfscert

4. The script to create the trusted provider in SharePoint.

The -Realm is what we put in as an identifier for ADFS(urn:sp13:contoso)

The -IdentifierClaimIs can only be ACCOUNT-NAME, EMAIL, or USER-PRINCIPAL-NAME.


$ap = New-SPTrustedIdentityTokenIssuer -Name “ADFS” -Description “ADFS provider” -Realm “urn:sp13:contoso” -ImportTrustCertificate $adfscert -signInURL “https://adfs.contoso.com/adfs/ls” -UseDefaultConfiguration -IdentifierClaimIs EMAIL

 We can add in more claims after the trusted provider is created. Example is below of adding a role claim.

$ap = Get-SPTrustedIdentityTokenIssuer “ADFS”
$ap.ClaimTypes.Add(“http://schemas.microsoft.com/ws/2008/06/identity/claims/role”)
$map = New-SPClaimTypeMapping “http://schemas.microsoft.com/ws/2008/06/identity/claims/role” -IncomingClaimTypeDisplayName “Role” -SameAsIncoming
$ap.AddClaimTypeInformation($map)
$ap.Update()

5. Associate the new trusted provider to your web application(s). In central admin, select Application Management, then Manage web applications. Select your web application, then Authentication Providers. For each zone, select Windows Authentication and Trust Identity provider. Both are required for the migration.


Migrate Users and Groups

The migration of users and groups are very similar to the migration scripts with a difference of the groups. The groups will change from Windows claim + SID to ADFS + SID. Other migrations are passing the unqualified name through the SAML token. The claim provider that is provisioned will not show you the SID just like Windows does either.

The PowerShell we will be running is Convert-SPWebApplication.

$wa = get-spwebapplication https://contoso
Convert-SPWebApplication -Identity $wa -From CLAIMS-WINDOWS -To CLAIMS-TRUSTED-DEFAULT -TrustedProvider $ap -RetainPermissions 

This will need to be run on each web application. After the conversion is done, you can disable the Windows Authentication.

People picker will have two results for all users that have an Email address in Active Directory. The ADFS results is the one we would want to select.


Resources

The Convert-SPWebApplication command cannot convert from Windows claims to SAML in SharePoint Server 2013 – https://support.microsoft.com/en-us/help/3042604/the-convert-spwebapplication-command-cannot-convert-from-windows-claim

Configure SAML-based claims authentication with AD FS in SharePoint 2013 – https://technet.microsoft.com/en-us/library/hh305235.aspx