Hi @Waqkas-Ahmed ,
You can use the CIM class Win32_UserProfile to enumerate as well as remove a local user profile. The cmdlets you will need are:
Get-CimInstance
to enumerate profiles
Remove-CimInstance
to delete profiles
Param (
[parameter(mandatory=$true,HelpMessage="Please enter the user account")]
[validateNotNullOrEmpty()]
[String]$localUser
)
Remove-LocalUser $localUser -Verbose
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $localUser } | Remove-CimInstance
Read-Host -Prompt "Press Enter to exit"
The LocalPath property of a Win32_UserProfile object is the directory path for the profile, c:\user\mike
, for example. We can use split to break the file path into substrings, at the backslashes. The -1
references the first substring starting from the end of the string or mike
in this example. So we can use the $localUser variable to filter the Get-CimInstance
cmdlet, and then pipe that into a Remove-CimInstance
cmdlet.
Hope this helps,
Mike Rodrick
Edutainer, ITProTV
**if the post above has answered the question, please mark the topic as solved.