• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

Remove profiles windows powershell script

I don't know if anyone is willing to do this or not.... but what I'm trying to do is create a script (I don't know scripting, but I'm getting books) that when run as an administrator (on a network) you give it a computer name, and it will remove all profiles from that machine... with the exception of accounts ending in -ADMIN or beginning with ZZP.

Is it possible to do?

Someone wrote one for me, but it gives a list of people and asks which ones you want to delete (numbered profiles and usernames). That takes too long for each computer as I'm dealing with 50+ computers at a time)
0
Nate
Asked:
Nate
  • 3
1 Solution
 
daveTechSearchCommented:
Perhaps you could post the script that you have.... perhaps we can 'fix' it.
0
 
NateAuthor Commented:
This is what I've got... after doing some research I found almost the same code online... only a few things were different.... the guy I got it from said he wrote it...

I don't know. Either way, would it be possible to take this code and modify it so it deletes all profiles except for profiles starting with ZZP and profiles ending in -admin
#Prompt for a computer to connect to 
$computer = Read-Host "Please enter a computer name" 
#Test network connection before making connection 
If ($computer -ne $Env:Computername) { 
    If (!(Test-Connection -comp $computer -count 1 -quiet)) { 
        Write-Warning "$computer is not accessible, please try a different computer or verify it is powered on." 
        Break 
        } 
    } 
Try {     
    #Verify that the OS Version is 6.0 and above, otherwise the script will fail 
    If ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt 6.0) { 
        Write-Warning "The Operating System of the computer is not supported.`nClient: Vista and above`nServer: Windows 2008 and above." 
        Break 
        } 
    } 
Catch { 
    Write-Warning "$($error[0])" 
    Break 
    }     
Do {     
#Gather all of the user profiles on computer 

Try { 
    [array]$users = Get-WmiObject -ComputerName $computer Win32_UserProfile -filter "LocalPath Like 'C:\\Users\\%'" -ea stop |
        Sort-Object  -property @{expression={(($_.localpath).replace('C:\Users\',''))}};
    } 
Catch { 
    Write-Warning "$($error[0]) " 
    Break 
    }     
#Cache the number of users 
$num_users = $users.count 
 
Write-Host -ForegroundColor Green "User profiles on $($computer):" 
 
    #Begin iterating through all of the accounts to display 
    For ($i=0;$i -lt $num_users; $i++) { 
        Write-Host -ForegroundColor Green "$($i): $(($users[$i].localpath).replace('C:\Users\',''))" 
        } 
    Write-Host -ForegroundColor Green "q: Quit" 
    #Prompt for user to select a profile to remove from computer 
    Do {     
        $account = Read-Host "Select a number to delete local profile or 'q' to quit" 
        #Find out if user selected to quit, otherwise answer is an integer 
        If ($account -NotLike "q*") { 
            $account = $account -as [int] 
            } 
        }         
        
        
    #Ensure that the selection is a number and within the valid range 
    Until (($account -lt $num_users -AND $account -match "\d") -OR $account -Like "q*") 
    If ($account -Like "q*") { 
        Break 
        } 
    Write-Host -ForegroundColor DarkCyan "Deleting profile: $(($users[$account].localpath).replace('C:\Users\',''))" 
    
    #Remove the local profile 
    ($users[$account]).Delete()
     
     Write-Host -ForegroundColor Green "Profile:  $(($users[$account].localpath).replace('C:\Users\','')) has been deleted" 
} 
#If user selects No, then quit the script     
Until ($userchoice -eq 1)

Open in new window

0
 
NateAuthor Commented:
Well... i've figured it out for myself....

I've modified the code and using if statements it asks if you want to auto delete. press y, enter the computer name, and it starts running.

#Prompt for a computer to connect to
$autodel = Read-Host "Do you want to Auto-Delete? (Y/N)"
$computer = Read-Host "Please enter a computer name"
#Test network connection before making connection 
If ($computer -ne $Env:Computername) { 
    If (!(Test-Connection -comp $computer -count 1 -quiet)) { 
        Write-Warning "$computer is not accessible, please try a different computer or verify it is powered on." 
        Break 
        } 
    } 
Try {     
    #Verify that the OS Version is 6.0 and above, otherwise the script will fail 
    If ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt 6.0) { 
        Write-Warning "The Operating System of the computer is not supported."
        Break 
        } 
    } 
Catch { 
    Write-Warning "$($error[0])" 
    Break 
    }     
Do {     
#Gather all of the user profiles on computer 

Try { 
    [array]$users = Get-WmiObject -ComputerName $computer Win32_UserProfile -filter "LocalPath Like 'C:\\Users\\%'" -ea stop |
        Sort-Object  -property @{expression={(($_.localpath).replace('C:\Users\',''))}};
    } 
Catch { 
    Write-Warning "$($error[0]) " 
    Break 
    }     
#Cache the number of users 
$num_users = $users.count 
 
Write-Host -ForegroundColor blue "User profiles on $($computer):" 
 
    #Begin iterating through all of the accounts to display 
    For ($i=0;$i -lt $num_users; $i++) { 
        if (($users[$i].localpath).replace('C:\Users\','').EndsWith("-admin") -eq $true) {
          Write-Host -ForegroundColor Green "$($i): $(($users[$i].localpath).replace('C:\Users\','')) * Administrator *"
         } elseif (($users[$i].localpath).replace('C:\Users\','').StartsWith("zzpet") -eq $true) {
                Write-Host -ForegroundColor Green "$($i): $(($users[$i].localpath).replace('C:\Users\','')) * Local Account *" 
            } else { 
                if($autodel.tolower() -eq "y") {
                  Write-Host -ForegroundColor Red "$($i): $(($users[$i].localpath).replace('C:\Users\',''))  Account being deleted"
                $users[$i].delete() 
                } else {
                if($autodel.tolower() -eq "n") {
                  Write-Host -ForegroundColor Green "$($i): $(($users[$i].localpath).replace('C:\Users\',''))"
                }
              }
          }
           
        } 
    Write-Host -ForegroundColor red "q: Quit" 
    #Prompt for user to select a profile to remove from computer 
    Do {     
        $account = Read-Host "Select a number to delete local profile or 'q' to quit" 
        #Find out if user selected to quit, otherwise answer is an integer 
        If ($account -NotLike "q*") { 
            $account = $account -as [int] 
            } 
        }         
        
        
    #Ensure that the selection is a number and within the valid range 
    Until (($account -lt $num_users -AND $account -match "\d") -OR $account -Like "q*") 
    If ($account -Like "q*") { 
        Break 
        } 
    Write-Host -ForegroundColor DarkCyan "Deleting profile: $(($users[$account].localpath).replace('C:\Users\',''))" 
    
    #Remove the local profile 
    ($users[$account]).Delete()
     
     Write-Host -ForegroundColor Green "Profile:  $(($users[$account].localpath).replace('C:\Users\','')) has been deleted" 
} 
#If user selects No, then quit the script     
Until ($userchoice -eq 1)

Open in new window

0
 
NateAuthor Commented:
I've modeified the code myself...
0

Featured Post

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now