PoshCode Logo PowerShell Code Repository

Difference between
modified post 6842 by Brian on Fri 14th Apr 18:49 and
original post 2477 by Boe Prox on Thu 27th Jan 17:49
Showold version | new version | both versions

    
11
<#
22
.SYNOPSIS
33
    Interactive menu that allows a user to connect to a local or remote computer and remove a local profile.
44
.DESCRIPTION
55
    Presents an interactive menu for user to first make a connection to a remote or local machine.  After making connection to the machine,
66
    the user is presented with all of the local profiles and then is asked to make a selection of which profile to delete. This is only valid
77
    on Windows Vista OS and above for clients and Windows 2008 and above for server OS.
88
.NOTES
99
    Name: Remove-LocalProfile
1010
    Author: Boe Prox
1111
    DateCreated: 26JAN2011
1212
.LINK
1313
    http://boeprox.wordpress.com
1414
    http://msdn.microsoft.com/en-us/library/ee886409%28v=vs.85%29.aspx
1515
.EXAMPLE
1616
Remove-LocalProfile
1818
Description
1919
-----------
2020
Presents a text based menu for the user to interactively remove a local profile on local or remote machine.
2121
#>
2323
#Prompt for a computer to connect to
2424
$computer = Read-Host "Please enter a computer name"
2525
#Test network connection before making connection
2626
If ($computer -ne $Env:Computername) {
2727
    If (!(Test-Connection -comp $computer -count 1 -quiet)) {
2828
        Write-Warning "$computer is not accessible, please try a different computer or verify it is powered on."
2929
        Break
3030
        }
3131
    }
3232
Try {
3333
    #Verify that the OS Version is 6.0 and above, otherwise the script will fail
34-
    If ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt 6.0) {
34+
    If ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt ("0:D2" -f 6.0)) {
3535
        Write-Warning "The Operating System of the computer is not supported.`nClient: Vista and above`nServer: Windows 2008 and above."
3636
        Break
3737
        }
3838
    }
3939
Catch {
4040
    Write-Warning "$($error[0])"
4141
    Break
4242
    }
4343
Do {
4444
#Gather all of the user profiles on computer
4545
Try {
4646
    $users = Get-WmiObject -ComputerName $computer Win32_UserProfile -filter "LocalPath Like 'C:\\Users\\%'" -ea stop
4747
    }
4848
Catch {
4949
    Write-Warning "$($error[0]) "
5050
    Break
5151
    }
5252
#Cache the number of users
5353
$num_users = $users.count
5555
Write-Host -ForegroundColor Green "User profiles on $($computer):"
5757
    #Begin iterating through all of the accounts to display
5858
    For ($i=0;$i -lt $num_users; $i++) {
5959
        Write-Host -ForegroundColor Green "$($i): $(($users[$i].localpath).replace('C:\Users\',''))"
6060
        }
6161
    Write-Host -ForegroundColor Green "q: Quit"
6262
    #Prompt for user to select a profile to remove from computer
6363
    Do {
6464
        $account = Read-Host "Select a number to delete local profile or 'q' to quit"
6565
        #Find out if user selected to quit, otherwise answer is an integer
6666
        If ($account -NotLike "q*") {
6767
            $account = $account -as [int]
6868
            }
6969
        }
7070
    #Ensure that the selection is a number and within the valid range
7171
    Until (($account -lt $num_users -AND $account -match "\d") -OR $account -Like "q*")
7272
    If ($account -Like "q*") {
7373
        Break
7474
        }
7575
    Write-Host -ForegroundColor Yellow "Deleting profile: $(($users[$account].localpath).replace('C:\Users\',''))"
7676
    #Remove the local profile
7777
    ($users[$account]).Delete()
7878
    Write-Host -ForegroundColor Green "Profile:  $(($users[$account].localpath).replace('C:\Users\','')) has been deleted"
8080
    #Configure yes choice
8181
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Remove another profile."
8383
    #Configure no choice
8484
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Quit profile removal"
8686
    #Determine Values for Choice
8787
    $choice = [System.Management.Automation.Host.ChoiceDescription[]] @($yes,$no)
8989
    #Determine Default Selection
9090
    [int]$default = 0
9292
    #Present choice option to user
9393
    $userchoice = $host.ui.PromptforChoice("Information","Remove Another Profile?",$choice,$default)
9494
    }
9595
#If user selects No, then quit the script
9696
Until ($userchoice -eq 1)

ContributeMost Recent Contributions (feed)

Contribute ... Next Page