Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Remove-UserProfile{
- <#
- .SYNOPSIS
- Removes User Profiles from a machine via WMI
- .Description
- This Cmdlet is used to remove user profiles from a machine when it is inconveinient to do so from the System menu. The script relies on Cmdlet Get-UserProfile. It is especially userful on Terminal Server/RDS machines where opening the system menu to delete user profiles can take hours (because reasons, I suppose).
- .PARAMETER UserID
- UserID to search for. Unlike Get-UserProfile, this cannot be left blank. If you intentionally want to remove all user profiles, or need to select multiple, you can use WMIs limited regex. Check this Link for more information: http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/13/use-the-like-operator-to-simplify-your-wql-queries.aspx
- .PARAMETER Computer
- Computer to delete user accounts from. Leaving Blank will default to 'localhost'.
- .PARAMETER Batch
- This Flag will suppress confirmation dialogs, as well change console output to the more redirect friendly Write-Output, rather than the Write-Host it otherwise uses.
- .PARAMETER OlderThan
- Filter user accounts based on last use time. Remember that this Cmdlet does not assume 'all users', so you must specify -UserID %.
- .Example
- Remove-UserProfile myuser remotemachine.mydomain.com
- Remove Users using positional parameters
- .Example
- Remove-UserProfile -UserID myuser -Computer remotemachine.mydomain.com
- remove users specifying with flags
- .Example
- Get-Content machinelist.txt | % {Remove-UserProfile -User myuser -Computer $_ -Batch} >> C:\Temp\UserRemoval.log
- Remove a user from a list of machines using the batch mode, seding output to a log file
- .Example
- Remove-UserProfile -OlderThan $((get-date).adddays(-14)) -UserID %
- Remove all user accounts that haven't been used in 14 days
- The '%' here is the WMI regex for anything (analogous to '*' (or '.*') in most regex) -- it is necessary because this Cmdlet never assumes all users
- .Notes
- Author: Keith Ballou
- Date: Oct 16, 2014
- #>
- [CmdletBinding()]
- param(
- [Parameter(Mandatory=$True)][string]$UserID,
- [Parameter(Mandatory=$False)][string]$Computer="LocalHost",
- [Parameter(Mandatory=$False)][datetime]$OlderThan=(get-date).adddays(1),
- [Parameter(Mandatory=$False)][switch]$Batch
- )
- #Make Sure Necessary Cmdlets Exist
- if(!(Get-Command Get-UserProfile -ErrorAction SilentlyContinue)){
- write-host -BackgroundColor "Black" -ForegroundColor "Red" "################################################################################"
- write-host -BackgroundColor "Black" -ForegroundColor "Red" "# "
- write-host -BackgroundColor "Black" -ForegroundColor "Red" "This Program Requires cmdlet ""Get-UserProfile"" "
- write-host -BackgroundColor "Black" -ForegroundColor "Red" "Find it here: "
- write-host -BackgroundColor "Black" -ForegroundColor "Red" "http://pastebin.com/wvUDki7p "
- write-host -BackgroundColor "Black" -ForegroundColor "Red" "# "
- write-host -BackgroundColor "Black" -ForegroundColor "Red" "################################################################################"
- break;
- }
- #to simplify query, if OlderThan was not specified, assume anything earlier than right now (plus a day to account for timezones, rounding, etc)
- #if(![bool]$OlderThan){
- # $OlderThan = (get-date).adddays(1)
- #}
- #This Part relies on another of my Cmdlets "get-UserPorfile" to simplify the code a bit
- #This Could be substituted with: $ProfileList = Get-WmiObject -Computer $Computer -Query "Select * From Win32_UserProfile where LocalPath like \\$UserID"
- #..............................: $ProfileList = $ProfileList | Where-Object -Property Special -eq $False
- #Additional logic may have to be added if WMI doesn't like 'localhost' as a ComputerName. Seems to work ok for me, but I wouldn't count on it.
- #Get_UserProfile includes this logic
- $ProfileList = Get-UserProfile -Verbose -UserID $UserID -Computer $Computer -ExcludeSystemAccounts -OlderThan $OlderThan
- #If no Users were found, exit
- if(!$ProfileList){
- Write-Warning "NO USER PROFILES WERE FOUND"
- RETURN;
- }
- #Confirmation Dialog if -Batch is not set
- if(!$Batch){
- Write-Warning "ABOUT TO REMOVE THE FOLLOWING USER ACCOUNTS"
- Foreach($User in $ProfileList){
- $User | Select SID,LocalPath,@{Label="Last Use Time";Expression={Convert-UTCtoDateTime $_.LastUseTime -ToLocal}}
- }
- $Title = "PROCEED?"
- $Message = "ARE YOU SURE YOU WANT TO REMOVE THE LISTED USER ACCOUNTS?"
- $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Removes User Accounts"
- $No = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Exits Script, No Changes Will be Made"
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
- $result = $host.ui.PromptForChoice($title, $message, $options, 1)
- switch ($result)
- {
- 0 {}
- 1 {return;}
- }
- }
- #Remove Users provided they are not currently Loaded
- Foreach($User in $ProfileList){
- if($User.Loaded){
- if(!$Batch){
- Write-Host -BackgroundColor "Black" -ForegroundColor "Red" "User Account " $User.LocalPath "is Currently in user on" $Computer ":`tSkipping"
- }
- else{
- Write-Output "User $($User.LocalPath) on $($Computer) was in use and could not be removed"
- }
- continue;
- }
- if(!$Batch){
- Write-Host -BackgroundColor "Blue" -ForegroundColor "Green" "Removing User $($UserID.LocalPath) from $($Computer)"
- }
- else{
- Echo "Deleting $($User.LocalPath) from $($Computer)"
- }
- $User.delete()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement