Remove Profiles from a local or remote computer


A common need for a Remote Desktop Services (RDS) and/or Citrix farm admin, is to remove local profiles from a server.
Another example for this, is the question posted a few days ago in the PowerShell.org forum here.

Funny thing, about 6 years ago, I wrote a vbscript that does that. It just doesn't filter by last used date.

Anyway, I decided to write the Remove-Profile PowerShell function:

You can use it to report all the profiles in the local computer:

Remove-Profile


To report all the profiles, except a specific profile:

Remove-Profile -Exclude Administrator


To report profiles last used in the past 90 days that would be deleted:

Remove-Profile -DaysOld 90 | Where-Object { $_.WouldBeRemoved }


To report against a remote computer:

Remove-Profile -ComputerName myRemoteServer


To report against a collection of remote computers, authenticating different credentials:

Remove-Profile -ComputerName $Computers -Credential $cred -DaysOld 90


To ignore the LastUseTime value in case the account never logged-on locally (e.g. an IIS ApplicationPool), use the IgnoreLastUseTime switch:

Remove-Profile -ComputerName WebServer01 -DaysOld 30 -IgnoreLastUseTime 


To really remove the profiles, use the -Remove switch:

Remove-Profile -DaysOld 90 -Remove


HTH,

\Martin.

Comments (1)

  1. Dean says:

    Thank you very much for this.

    After a lot of searching on the internet, this is the closest I have come to being able to remove profiles safely without affecting NT authority accounts.

    I’m having some problems excluding this apppool account from the filter.
    Account : IIS APPPOOL\.NET v4.5 Classic
    LocalPath : C:\Users\.NET v4.5 Classic
    LastUseTime : 21/06/2018 18:25:49
    Loaded : False
    WouldBeRemoved : True

    I have tried adding to the if statement but I’m not experienced enough yet. Could this error be anything to do with it?

    Exception calling “Translate” with “1” argument(s): “Some or all identity references could not be translated.”
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Remove-Profile\Remove-Profile.psm1:44 char:17
    + $prf = [pscustomobject]@{
    + ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdentityNotMappedException

Skip to main content