PowerShell for Windows Admins

Dec 12 2013   9:00AM GMT

Deleting domain specific profiles

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I had a question regarding these posts:

http://msmvps.com/blogs/richardsiddaway/archive/2012/06.aspx

http://msmvps.com/blogs/richardsiddaway/archive/2013/12/10/1810778.aspx

The question came from someone needing to clean up a set of domain specific profiles.

You can adapt the code in the articles to identify the domain specific profiles like this:

$domain = “mydomain”
Get-CimInstance -ClassName Win32_UserProfile -Filter “Special = ‘$false’” |
select LocalPath, SID, LastUseTime,
@{N=”User”; E={Get-CimInstance -Class Win32_UserAccount -Filter “SID = ‘$($_.SID)’” | select -ExpandProperty Caption}} |
where User -Like “$domain\*”

Once you’ve identified them and ensured that your filters are giving you the correct results you can run this:

$domain = “mydomain”
Get-CimInstance -ClassName Win32_UserProfile -Filter “Special = ‘$false’” |
select LocalPath, SID, LastUseTime,
@{N=”User”; E={Get-CimInstance -Class Win32_UserAccount -Filter “SID = ‘$($_.SID)’” | select -ExpandProperty Caption}} |
where User -Like “$domain\*” |
foreach {
Get-CimInstance -ClassName Win32_UserProfile -Filter “SID = ‘$($_.SID)’” |
Remove-CimInstance -WhatIf

}

NOTICE I’ve left –whatif on the Remove-CimInstance.  It gives you an additional check before you finally commit to deleting the profiles

Alternatively, and better still, put the first set of code into an advanced function and then you can do this:

function get-domainprofile{
[CmdletBinding()]
param (
[string]$domain = “mydomain”
)
Get-CimInstance -ClassName Win32_UserProfile -Filter “Special = ‘$false’” |
select LocalPath, SID, LastUseTime,
@{N=”User”; E={Get-CimInstance -Class Win32_UserAccount -Filter “SID = ‘$($_.SID)’” | select -ExpandProperty Caption}} |
where User -Like “$domain\*”
}

get-domainprofile | foreach {
Remove-CimInstance -Query “Select * FROM Win32_UserProfile WHERE SID = ‘$($_.SID)’” -WhatIf
}

Again I’ve left –whatif in place. Remove it when you’re happy with the tests

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: