r/PowerShell • u/adbertram • Jul 23 '19
Use PowerShell delete a user profile (step-by-step guide)
https://adamtheautomator.com/powershell-delete-user-profile/?utm_source=reddit&utm_medium=social&utm_campaign=newblogpostnotifications10
Jul 24 '19
delprof2...
8
5
u/dextersgenius Jul 24 '19
DelProf2 is no longer supported. Hasn't been updated in 6 years and has some issues with Windows 10. The author himself stated that he can't be arsed to fix then as he claims its a Microsoft issue and he shouldn't have to work around it. Either way, I wouldn't recommend using that on production Windows 10 machines, especially if you're using UE-V profiles.
0
u/adhdasf23423 Jul 24 '19
sucks
1
Jul 24 '19
Why make things needlessly complex with a long script?
4
u/adbertram Jul 24 '19
One line of PowerShell isn’t a long script.
0
Jul 24 '19
Sure if you are just deleting one profile
1
u/adbertram Jul 24 '19
It honestly would still be only 1-2 lines with PowerShell. You'd simply not limit it to just one like I did.
1
u/adhdasf23423 Jul 25 '19
Ok it doesn't suck, but you're suggesting replacing a 1 line script with... another 1 line script.
3
u/xCharg Jul 24 '19
Used this method once, it's easy enough and does it job fairly quick.
GPO method scares me a little bit after that huge fuck up by Microsoft at their 1809 release.
Btw - typo here:
You'll notice many useful tidbits of information like LastUseTime, SID and son.
2
u/nemanja_jovic Jul 24 '19
This also works for remote removals.
https://gallery.technet.microsoft.com/Remove-User-Profile-6f1b53b4?redir=0
2
u/B9BRF Jul 24 '19
I use powershell for profile deletes and have created a script that will backup the users sticky notes for both w7 and w10 devices on a single user profile
2
u/Vortex100 Jul 24 '19
This:
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'UserA' } | Remove-CimInstanceIs not ideal. User profile paths do not change if the user object is renamed, and what about temp profiles?
Do this instead
if (-not [System.String]::IsNullOrEmpty($UserName))
{
$SID = (Get-ADUser -Identity $UserName).SID.Value
$Filter = "Special=False and SID='$SID'"
}
else
{
$Filter = "Special=False"
}
Get-CimInstance -Computername $ComputerName -Class Win32_UserProfile -Filter $FilterOr the oneliner:
Get-CimInstance -Computername $ComputerName -Class Win32_UserProfile -Filter "Special=False and SID=$((Get-ADUser -Identity $UserName).SID.Value)"1
1
1
20
Jul 24 '19
[removed] — view removed comment
9
u/adbertram Jul 24 '19
PowerShell all the things!
9
Jul 24 '19
[removed] — view removed comment
2
u/adbertram Jul 24 '19
I agree. There are always more than one way to skin a cat. It all depends on context.
5
u/Emiroda Jul 24 '19
As you said, right tool for the job.
So GPO for domain machines you are certain will be reachable, where the GPO fits your needs.
Something else for those that might not be. Non-domain joined DMZ servers, road warrior laptops, etc. I use SCCM to run my PowerShell scripts, either as the new CMScript feature or as CIs.
The only thing you can do entirely wrong is going "this looks nice, I'm going to implement this in production right now". Slow down, see the value being presented, research if there's any alternatives (in this case you'd find the GPO that does this) and then decide what's best in your environment.
1
u/jatorres Jul 24 '19
Really? I thought PowerShell was the end all, be all of Windows automation? Any guides / best practices for a GPO that would do this?
4
Jul 24 '19
GP is like a shotgun that can double as a rifle. PS is more like a snipper rifle that can (sometimes) double as a shotgun.
4
u/billy_teats Jul 24 '19
A shotgun is not going to hit every single bird in a flock. PS will for sure.
GP hunts. It’s going to find you and get you. Just like everyone else. If you’re around when PS goes off though, ain’t nothin you can do to stop it
0
u/adbertram Jul 24 '19
I disagree. Sure, if you forget to add a filter of some kind, PS can destroy the world but one of the best quotes I've heard is "Automation is the only way to fail at scale". With great power comes great responsibility. :)
0
u/vibrants Jul 24 '19
Disable WinRM
2
u/billy_teats Jul 24 '19
Remove your computer from the domain. Unplug it.
There’s ways around everything
2
19
Jul 24 '19 edited Jul 24 '19
[removed] — view removed comment
3
u/jeshii Jul 24 '19
I used to have a server dedicated to running powershell scripts on a certain schedule via task scheduler when I was a sysadmin. Not that I disagree with using GPO (since its in the domain and therefore automatically redundant), but I think it's not exactly fair to say that powershell scripts can't be run automatically.
0
Jul 24 '19
[deleted]
2
Jul 24 '19
[removed] — view removed comment
0
u/Aalkfk Jul 24 '19 edited Jul 24 '19
Wouldn’t DSC be the better approach for the automation of „PS-Scripts“?🤔 I think it Is a good mix between PS and GP.
3
u/halbaradkenafin Jul 24 '19
Not really. DSC is all about configuration management, it's not a task runner (though it can do that). You want to use DSC to say "my server should be configured in this way, make it so (and possibly keep it that way)" and it goes off and does its thing.
2
u/Aalkfk Jul 24 '19
Sure, but isn't it also a configuration that certain profiles should be deleted After X unused days? Of course, it is not like task planner and also involves more effort. But if you want to go towards the shell, it makes more sense than the GP (at least as soon as you know if and how the new versions are backwards compatible).
1
Jul 24 '19
[removed] — view removed comment
2
u/halbaradkenafin Jul 24 '19
The problem I've got with GPO is that it's not very visible. If I want to find out how a node is configured I have to run RSOP or other tools, and tracking down where things are configured is a pain. DSC has the advantage that everything is in code, so it's all in source control, I can control how I roll out changes and updates a lot easier using a pipeline to a pull server, I can use Pester to test it in an easier way than GPO.
GPO certainly still has a place but I'd be moving most of my general machine settings over to DSC, your user specific settings aren't as manageable by DSC so they are best off staying configured by GPO.
→ More replies (0)1
u/Aalkfk Jul 24 '19
I'm with you on this one. GP is the easiest way (If there are no problems in combination with Windows 10). Just wanted to show that there is a another option. :-)
→ More replies (0)1
u/JasonG81 Jul 24 '19
Whats dsc?
2
u/Aalkfk Jul 25 '19
It is best to read a few articles about this, they are much more detailed. I would describe it as a (currently still failed) attempt to replace the GP with something that allows management over all devices (of they Switch To PSCore also Linux,...). You specify certain parameters in a file and the client pulls them or gets them pushed by the server and then processes them with PS modules installed on the PC.
1
1
u/lordv0ldemort Jul 24 '19
Although GPO would be the preferred method, I do enjoy seeing things done the PS way as well.
It’s not if you should do it, it’s if you could do it.
3
Jul 24 '19
[removed] — view removed comment
2
u/lordv0ldemort Jul 25 '19
Right. Again, I do agree with GPO being the preferred method, just stating that I enjoy seeing the PowerShell way as well.
2
u/Swarfega Jul 24 '19