Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Deleting Old User Profiles

$
0
0

Just so you understand the problem with your script. You are generating a PSObject from Get-WMIObject, so the object doesn't have a .Delete() method (even if it did, it would just manipulate the PSObject, not the profiles). As soon as you use Select-Object, you are generating a PSObject. That is why the example Raymond provided worked using an implicit foreach (available in v3 and above). If you are using Powershell V2, you would have to explicitly use a foreach:

Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user'} | foreach{$_.Delete()}

To help you understand further, using your $profile variable above, do:

$profile | foreach{ $_.GetType()}

and then try:

Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user'} | foreach{$_.GetType()}

You should also look at some of the Win32_UserProfile properties such as Special (indicates a SYSTEM, NETWORK, ADMINISTRATOR profile) and Loaded (indicates a profile is loaded in registry) to do your filtering. You are specifically looking for "Administrator" and not other system profiles in your filter plus it should be done in WMI filtering, not after. See what is returned with this:

gwmi Win32_UserProfile -Filter "Loaded=False And Special=False"

Lastly, I've had issues deleting profiles in Terminal Server environments because of long path\files which AppData contains temporary internet files that I usually find the culprit. Attached is a script that I wrote for our Citrix\TS folks and it seemed to work without issue and provided logging indicating why a profile was skipped or deleted. The script is writted to support -WhatIf, so make sure you test, then test, test again in a non-production environment:

PS C:\> C:\Scripts\Delete-Profile.ps1 -WhatIf
Attachments:
You must be logged in to view attached files.

Viewing all articles
Browse latest Browse all 13067

Trending Articles