How do I Delete User Profile

Hi There,

Please help me get the correct code for deleting the User local profile with specific naming convention .
Ex. I want to delete user names logged in with abc_xyz while abc_ is constant and xyz will have different names of users.

My code is :
Get-WMIObject Win32_UserProfile |where-object {$.localpath -like "c:\users\abc*"}

But i do not get it right also i am not able to delete these profiles.
Please help. !

Your query just lists the profiles on that machine.
The way I remove profiles is with:

$username = [yourusername]
$localProfilePath = "C:\\Users\\$Username"
$WMIQuery = "SELECT * FROM Win32_UserProfile WHERE localpath = '$localProfilePath'"
$profile = Get-WmiObject -Query $WMIQuery -ComputerName $Computer 
Remove-WmiObject -InputObject $profile  

Thank you,

But I want to delete user names logged in with abc_xyz while abc_ is constant and xyz will have different names of users.

Example : abc_ankit
abc_gupta500
abc_khand590

How do I get that working in it . ?

Try this…

foreach($user In “abc”,“xyz”)
{
$username = “abc_$user”
Write-host “$username”
$localProfilePath = “C:\Users\$Username”
$WMIQuery = “SELECT * FROM Win32_UserProfile WHERE localpath = ‘$localProfilePath’”
$profile = Get-WmiObject -Query $WMIQuery -ComputerName $Computer
Remove-WmiObject -InputObject $profile
}

OK But how do i get all the logged in user details in $user .
I mean to say There are many users logged in with ID similar to abc_xyz123 also with other names such as doma500 or sa_account

Among all I need to filter only users whose profile begins with “abc_” and delete it .
Please get me some help please.

You would need to loop through the profiles with something like:

$profiles = Get-WmiObject Win32_UserProfile -ComputerName $Computer | where {$_.localpath -like "C:\Users\abc_*"}
foreach($P in $profile){
    Remove-WmiObject -InputObject $p
}

This would remove any profile that starts with abc_
**I have not tested this.

It throws an error :

Remove-WmiObject : Cannot bind parameter ‘InputObject’. Cannot convert value “C
:\Users\Microsoft.PowerShellISE_
profile.ps1” to type “System.Management.ManagementObject”. Error: "Invalid para
meter "
At C:\Users\Untitled2.ps1:4 char:31

  • Remove-WmiObject -InputObject <<<< $p
    • CategoryInfo : InvalidArgument: (:slight_smile: [Remove-WmiObject], Paramet
      erBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh
      ell.Commands.RemoveWmiObject

Please help me on this

Hi Ankit,

If you just tried to copy/paste that code and ran it, then please be really careful.
I think it is not working is because I left the -computername parameter in the get-wmiobject.

Running code you do not fully understand what its doing can cause major issues. So please in the future, read through the code. Try and understand what its doing, and evaluate the risk.
If you don’t understand it, then ask us here. I am sure people here will be more than happy to help you understand the code.

yes, that is right.

I am very new to PSS and willing to learn more .
Can you please correct the code and help me get it through Please ?

There are pre-built scripts via MS website and this site as well as others which cover this use case.

However, I cannot over emphasize to you enough what has been already said. Don’t just arbitrarily run anyone’s code you do not fully understand in a production environment, and even in test be cautious and test on systems which you don’t mind destroying or having issues on. Meaning ones you may have to rebuild if you get it wrong.

You should really take a quick PowerShell online course using Microsoft Virtual Academy, before doing any real work with PowerShell, especially this sort of thing, so, that you have a real baseline understanding. It’s good to experiment, but experiment in a safe zone.

Simple search for training using your favorite search engine

‘Microsoft virtual academy’ PowerShell
youtube.com/results?search_query=powershell+ise+scripting+for+beginners
youtube.com/results?search_query=powershell+for+beginners+

Delete Unused user Profiles on local machine (PowerShell)

gallery.technet.microsoft.com/scriptcenter/Delete-Unused-user-4047f41d

Script Delete user profiles over multiple servers v2

gallery.technet.microsoft.com/scriptcenter/Delete-user-profiles-over-05348eef

Other resoruces explaining the same use case.

Use PowerShell to remove local profiles | Learn Powershell | Achieve More
powershell.org/forums/topic/deleting-old-user-profiles
Powershell: Script to delete Windows User Profiles on Windows 7/Windows 2008 R2
Because It Should Be Documented: PowerShell: Managing User Profiles on Remote Machines

#One of the ways profiles older than two days were deleted easily by using below script.

$d = (get-date).AddDays(-2)
$prof = @(ls c:\users -Exclude “Administrator”)
$tobedeleted=@()
foreach($i in $prof){
if($i.lastwritetime -lt $d){
$tobedeleted += $i }
}
$tobedeleted

foreach($p in $tobedeleted){
cmd.exe /c “rmdir /s /q $p”
}