# Deleting Profiles Remotely

**URL:** <https://forums.powershell.org/t/deleting-profiles-remotely/13901>\
**Category:** PowerShell Help\
**Created:** [March 3, 2020, 8:26am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901 "2020-03-03T08:26:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [March 3, 2020, 8:26am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/1 "2020-03-03T08:26:34Z")

</div>

Hi… Looking for a script to delete all profiles remotely

We want to clear all profiles so the machine can be used for another user. The machines will be offsite

Very new to powershell so any help would be great

Thanks

Dan

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 3, 2020, 8:41am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/2 "2020-03-03T08:41:14Z")

</div>

Have you searched for “Delete Profiles Powershell”?

> **[Manage Windows User Profiles With PowerShell - How To - business.com](https://www.business.com/articles/powershell-manage-user-profiles/)**
>
> Managing Windows user profiles can be time consuming and tedious. Here are some tips on how to leverage PowerShell.

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [March 3, 2020, 9:04am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/3 "2020-03-03T09:04:33Z")

</div>

The Script is just for local use, Need to delete from a networked machine

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [March 3, 2020, 10:10am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/4 "2020-03-03T10:10:13Z")

</div>

You can remove wmi win32\_userprofile objects. There’s a group policy to do it too.

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [March 3, 2020, 10:12am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/5 "2020-03-03T10:12:01Z")

</div>

Thanks for the answer is there a full script I could copy

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [March 3, 2020, 11:37am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/6 "2020-03-03T11:37:10Z")

</div>

The link Rob referenced has your answer:

`Get-WmiObject -Class Win32_UserProfile –ComputerName CLIENT1,CLIENT2 | where {$.LocalPath.split('')[-1] -eq 'Administrator.CLIENT1'} | foreach {$.Delete()}`

To delete all profiles, you will want to modify to something like this so you DONT delete the System Profiles:

`Get-WmiObject -ClassName Win32_UserProfile –ComputerName CLIENT1,CLIENT2 | Where-Object {!($.LocalPath -Match 'windows')} | foreach {$.Delete()}`

I am thinking that nuking the system profiles might be a bad idea.

You can view all profiles as such:

`(Get-WmiObject -ClassName Win32_UserProfile –ComputerName COMPUTER1).LocalPath`

&nbsp;

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 3, 2020, 4:01pm UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/7 "2020-03-03T16:01:14Z")

</div>

Did you try to search for the topic before you started asking?

[https://adamtheautomator.com/powershell-delete-user-profile/](https://adamtheautomator.com/powershell-delete-user-profile/)

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [March 4, 2020, 3:43am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/8 "2020-03-04T03:43:02Z")

</div>

Thank you, Script ran without any errors but didn’t delete the profile from c:\

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [March 4, 2020, 3:56am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/9 "2020-03-04T03:56:57Z")

</div>

$Computer = Read-Host "Please Enter Computer Name: "  
$user = Read-Host "Enter User ID: "

Invoke-Command -ComputerName $computer -ScriptBlock {  
param($user)  
$localpath = 'c:\users' + $user  
Get-WmiObject -Class Win32\_UserProfile | Where-Object {$\_.LocalPath -eq $localpath} |  
Remove-WmiObject  
} -ArgumentList $user

&nbsp;

This seems to work on local machines but not remote any ideas?

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [March 4, 2020, 5:54am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/10 "2020-03-04T05:54:06Z")

</div>

I have got this script to work removing 1 profile is there anyway to put exclusions in it so we can wipe all profiles except the built in accounts for example public

$Computer = Read-Host "Please Enter Computer Name: "  
$user = Read-Host "Enter User ID: "

Invoke-Command -ComputerName “” -ScriptBlock {  
param($user)  
$localpath = 'c:\users' + “”  
Get-WmiObject -Class Win32\_UserProfile | Where-Object {$\_.LocalPath -eq $localpath} |  
Remove-WmiObject  
} -ArgumentList $user

---

<div class="post-metadata">

**Author:** ![danielburt123](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@danielburt123](https://forums.powershell.org/u/danielburt123)\
**Post date:** [March 4, 2020, 7:22am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/11 "2020-03-04T07:22:05Z")

</div>

So this is nearly exactly what I need, How would I add an option for all profiles rather than deleting 1 by 1

&nbsp;

#Prompt for a computer to connect to  
$computer = Read-Host “Please enter a computer name”  
#Test network connection before making connection  
If ($computer -ne $Env:Computername) {  
If (!(Test-Connection -comp $computer -count 1 -quiet)) {  
Write-Warning “$computer is not accessible, please try a different computer or verify it is powered on.”  
Break  
}  
}  
Try {

# #Verify that the OS Version is 6.0 and above, otherwise the script will fail

# If ((Get-WmiObject -ComputerName $computer Win32\_OperatingSystem -ea stop).Version -lt 6.0) {

# Write-Warning “The Operating System of the computer is not supported.`nClient: Vista and above`nServer: Windows 2008 and above.”

# Break

# }

}  
Catch {  
Write-Warning “$($error[0])”  
Break  
}  
Do {  
#Gather all of the user profiles on computer  
Try {  
[array]$users = Get-WmiObject -ComputerName $computer Win32\_UserProfile -filter “LocalPath Like ‘C:\Users\%’” -ea stop  
}  
Catch {  
Write-Warning "$($error[0]) "  
Break  
}  
#Cache the number of users  
$num\_users = $users.count

Write-Host -ForegroundColor Green “User profiles on $($computer):”

#Begin iterating through all of the accounts to display  
For ($i=0;$i -lt $num\_users; $i++) {  
Write-Host -ForegroundColor Green “$($i): $(($users[$i].localpath).replace(‘C:\Users',’'))”  
}  
Write-Host -ForegroundColor Green “q: Quit”  
#Prompt for user to select a profile to remove from computer  
Do {  
$account = Read-Host “Select a number to delete local profile or ‘q’ to quit”  
#Find out if user selected to quit, otherwise answer is an integer  
If ($account -NotLike “q\*”) {  
$account = $account -as [int]  
}  
}  
#Ensure that the selection is a number and within the valid range  
Until (($account -lt $num\_users -AND $account -match “\d”) -OR $account -Like “q\*”)  
If ($account -Like “q\*”) {  
Break  
}  
Write-Host -ForegroundColor Yellow “Deleting profile: $(($users[$account].localpath).replace(‘C:\Users',’'))”  
#Remove the local profile  
($users[$account]).Delete()  
Write-Host -ForegroundColor Green “Profile: $(($users[$account].localpath).replace(‘C:\Users',’')) has been deleted”

#Configure yes choice  
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”,“Remove another profile.”

#Configure no choice  
$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”,“Quit profile removal”

#Determine Values for Choice  
$choice = [System.Management.Automation.Host.ChoiceDescription] @($yes,$no)

#Determine Default Selection  
[int]$default = 0

#Present choice option to user  
$userchoice = $host.ui.PromptforChoice(“”,“Remove Another Profile?”,$choice,$default)  
}  
#If user selects No, then quit the script  
Until ($userchoice -eq 1)

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [March 4, 2020, 9:37am UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/12 "2020-03-04T09:37:28Z")

</div>

The filtering can be done more efficiently. First, let’s look at all properities:

```
PS C:\Users\rasim> Get-CimInstance -ClassName Win32_UserProfile

AppDataRoaming : Win32_FolderRedirectionHealth
Contacts : Win32_FolderRedirectionHealth
Desktop : Win32_FolderRedirectionHealth
Documents : Win32_FolderRedirectionHealth
Downloads : Win32_FolderRedirectionHealth
Favorites : Win32_FolderRedirectionHealth
HealthStatus : 3
LastAttemptedProfileDownloadTime : 
LastAttemptedProfileUploadTime : 
LastBackgroundRegistryUploadTime : 
LastDownloadTime : 
LastUploadTime : 
LastUseTime : 3/4/2020 8:53:56 AM
Links : Win32_FolderRedirectionHealth
Loaded : True
LocalPath : C:\Users\rasim
Music : Win32_FolderRedirectionHealth
Pictures : Win32_FolderRedirectionHealth
RefCount : 
RoamingConfigured : False
RoamingPath : 
RoamingPreference : 
SavedGames : Win32_FolderRedirectionHealth
Searches : Win32_FolderRedirectionHealth
SID : S-1-5-21-2370741873-3768727752-2996739931-1001
Special : False
StartMenu : Win32_FolderRedirectionHealth
Status : 0
Videos : Win32_FolderRedirectionHealth
PSComputerName : 

AppDataRoaming : Win32_FolderRedirectionHealth
Contacts : Win32_FolderRedirectionHealth
Desktop : Win32_FolderRedirectionHealth
Documents : Win32_FolderRedirectionHealth
Downloads : Win32_FolderRedirectionHealth
Favorites : Win32_FolderRedirectionHealth
HealthStatus : 3
LastAttemptedProfileDownloadTime : 
LastAttemptedProfileUploadTime : 
LastBackgroundRegistryUploadTime : 
LastDownloadTime : 
LastUploadTime : 
LastUseTime : 3/4/2020 8:53:56 AM
Links : Win32_FolderRedirectionHealth
Loaded : True
LocalPath : C:\WINDOWS\ServiceProfiles\NetworkService
Music : Win32_FolderRedirectionHealth
Pictures : Win32_FolderRedirectionHealth
RefCount : 
RoamingConfigured : False
RoamingPath : 
RoamingPreference : 
SavedGames : Win32_FolderRedirectionHealth
Searches : Win32_FolderRedirectionHealth
SID : S-1-5-20
Special : True
StartMenu : Win32_FolderRedirectionHealth
Status : 0
Videos : Win32_FolderRedirectionHealth
PSComputerName : 

AppDataRoaming : Win32_FolderRedirectionHealth
Contacts : Win32_FolderRedirectionHealth
Desktop : Win32_FolderRedirectionHealth
Documents : Win32_FolderRedirectionHealth
Downloads : Win32_FolderRedirectionHealth
Favorites : Win32_FolderRedirectionHealth
HealthStatus : 3
LastAttemptedProfileDownloadTime : 
LastAttemptedProfileUploadTime : 
LastBackgroundRegistryUploadTime : 
LastDownloadTime : 
LastUploadTime : 
LastUseTime : 3/4/2020 8:53:56 AM
Links : Win32_FolderRedirectionHealth
Loaded : True
LocalPath : C:\WINDOWS\ServiceProfiles\LocalService
Music : Win32_FolderRedirectionHealth
Pictures : Win32_FolderRedirectionHealth
RefCount : 
RoamingConfigured : False
RoamingPath : 
RoamingPreference : 
SavedGames : Win32_FolderRedirectionHealth
Searches : Win32_FolderRedirectionHealth
SID : S-1-5-19
Special : True
StartMenu : Win32_FolderRedirectionHealth
Status : 0
Videos : Win32_FolderRedirectionHealth
PSComputerName : 

AppDataRoaming : Win32_FolderRedirectionHealth
Contacts : Win32_FolderRedirectionHealth
Desktop : Win32_FolderRedirectionHealth
Documents : Win32_FolderRedirectionHealth
Downloads : Win32_FolderRedirectionHealth
Favorites : Win32_FolderRedirectionHealth
HealthStatus : 3
LastAttemptedProfileDownloadTime : 
LastAttemptedProfileUploadTime : 
LastBackgroundRegistryUploadTime : 
LastDownloadTime : 
LastUploadTime : 
LastUseTime : 3/4/2020 8:53:56 AM
Links : Win32_FolderRedirectionHealth
Loaded : True
LocalPath : C:\WINDOWS\system32\config\systemprofile
Music : Win32_FolderRedirectionHealth
Pictures : Win32_FolderRedirectionHealth
RefCount : 1
RoamingConfigured : False
RoamingPath : 
RoamingPreference : 
SavedGames : Win32_FolderRedirectionHealth
Searches : Win32_FolderRedirectionHealth
SID : S-1-5-18
Special : True
StartMenu : Win32_FolderRedirectionHealth
Status : 0
Videos : Win32_FolderRedirectionHealth
```

Next, cleanup the output to see what are relevant filters can be used:

PS C:\Users\rasim\> Get-CimInstance -ClassName Win32\_UserProfile | Select LocalPath, Special, Loaded, RoamingConfigured

```
LocalPath Special Loaded RoamingConfigured
--------- ------- ------ -----------------
C:\Users\rasim False True False
C:\WINDOWS\ServiceProfiles\NetworkService True True False
C:\WINDOWS\ServiceProfiles\LocalService True True False
C:\WINDOWS\system32\config\systemprofile True True False
```

The output shows the Special or system profiles. Next, you probably should not be deleting profiles that are loaded, being utilized currently. Lastly, you specified it has to be local path, so you want to ensure that it is not a Roaming profile:

```
PS C:\Users\rasim> Get-CimInstance -ClassName Win32_UserProfile -Filter "Special <> 'True' and RoamingConfigured = 'False' and LocalPath LIKE '%sim%'" | 
Select @{Name='Profile';Expression={($_.LocalPath -split '\\')[-1] }}, LocalPath

Profile LocalPath     
------- ---------     
rasim C:\Users\rasim
```

As mentioned above, I would also filter Loaded (e.g. “and Loaded = ‘False’”), but I am using the profile. As you can see everything is filtered and now we can use a calculated expression to just get the profile name. The [-1] represents the UpperBound value, the highest part of the array. To answer your last question,

```
$profiles = @()
$profiles += Get-CimInstance -ClassName Win32_UserProfile -Filter "Special <> 'True' and RoamingConfigured = 'False' and LocalPath LIKE '%sim%'" | 
            Select-Object -Property @{Name='Profile';Expression={($_.LocalPath -split '\\')[-1] }}, LocalPath

$profiles += [pscustomobject]@{Profile='All';LocalPath='All'}

$profiles = $profiles | foreach {$i=0} {$_ | Add-Member Index ($i++) -PassThru}

foreach ($profile in $profiles) {
    Write-Host -ForegroundColor Green ('{0}) {1}' -f $profile.Index, $profile.Profile)
}
```

Found the index stuff here, but looks similar to how you were referencing the item to delete in your above post:

> **[Finding the index of an object within an array by property value using...](https://powershellone.wordpress.com/2015/08/04/finding-the-index-of-an-object-within-an-array-by-property-value-using-powershell/)**
>
> Today I was looking for an option to find the index of an object within an array of objects. Using an array of one dimensional objects like integers this is as simple as using the static IndexOf me…

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/deleting-profiles-remotely/13901/13 "2024-05-16T20:30:23Z")

</div>


