Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PowerShell Script to delete 100+ local user accounts from 50+ desktops

  • 16-09-2018 4:51pm
    #1
    Registered Users, Registered Users 2 Posts: 810 ✭✭✭


    Hi,
    I am looking for a powershell script to delete 100+ Local user accounts from 50+ desktops that are in the Domain. Instead of logging into each individual PC's and deleting the users one by one, I prefer to run a powershell script from somewhere like the domain controller to delete the users from each of these desktops.

    I have the details in an excel spreadsheet as below

    th_099541774_PowerShell_122_19lo.JPG

    Any other forums that you suggest, for me to get help regarding this?
    Tagged:


Comments

  • Registered Users, Registered Users 2 Posts: 3,036 ✭✭✭BailMeOut


    you can delete local accounts using GPO.


  • Registered Users, Registered Users 2 Posts: 810 ✭✭✭TestLink


    BailMeOut wrote: »
    you can delete local accounts using GPO.

    May I know how?

    PowerShell:
    I have the following code for PS that delete powershell, if I give the computer name. I would like to script to read from an excel sheet and delete user accounts, if this is possible.

    Any help?

    <#
    .SYNOPSIS
    Interactive menu that allows a user to connect to a local or remote computer and remove a local profile.
    .DESCRIPTION
    Presents an interactive menu for user to first make a connection to a remote or local machine. After making connection to the machine,
    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
    on Windows Vista OS and above for clients and Windows 2008 and above for server OS.
    .NOTES
    Name: Remove-LocalProfile
    Author: Boe Prox
    DateCreated: 26JAN2011
    .LINK
    https://boeprox.wordpress.com
    http://msdn.microsoft.com/en-us/library/ee886409%28v=vs.85%29.aspx
    .EXAMPLE
    Remove-LocalProfile

    Description
    Presents a text based menu for the user to interactively remove a local profile on local or remote machine.
    #>

    #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
    }
    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 Yellow "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"

    #Configure yes choice
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Remove another profile."

    #Configure no choice
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Quit profile removal"

    #Determine Values for Choice
    $choice = [System.Management.Automation.Host.ChoiceDescription[]] @($yes,$no)

    #Determine Default Selection
    [int]$default = 0

    #Present choice option to user
    $userchoice = $host.ui.PromptforChoice("","Remove Another Profile?",$choice,$default)
    }
    #If user selects No, then quit the script
    Until ($userchoice -eq 1)


  • Registered Users, Registered Users 2 Posts: 3,036 ✭✭✭BailMeOut


    Computer Configuration / Preferences / Control Panel Settings / Local Users and Groups / Delete user

    (see screenshot)

    461637.jpg


  • Registered Users, Registered Users 2 Posts: 810 ✭✭✭TestLink


    BailMeOut wrote: »
    Computer Configuration / Preferences / Control Panel Settings / Local Users and Groups / Delete user

    There are around 150+ desktops where local users have top be deleted. I need a way to automate the process via a script, rather than doing it computer by computer.


  • Registered Users, Registered Users 2 Posts: 3,036 ✭✭✭BailMeOut


    TestLink wrote: »
    There are around 150+ desktops where local users have top be deleted. I need a way to automate the process via a script, rather than doing it computer by computer.

    My process will work. Just add all the usernames to a GPO as per my screenshot and then add this GPO to an OU (or OU's) where your computers are located in AD. If the user does not exist on a machine then nothing will happen, if the user is on the list it will be deleted. You can also click the 'Apply once and do not reapply' box under 'Common'.

    461641.jpg


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 810 ✭✭✭TestLink


    May I know whether the following code would work?

    # Path of the CSV file
    $hostdetail = Import-CSV C:\Users\j\Desktop\Test\hosts.csv

    $scriptBlock = {
    Remove-LocalUser -Name $args[0]
    }

    ForEach ($item in $hostdetail) {
    $hostname = $($item.hostname)
    $username = $($item.username)
    $computer = $hostname

    #Test network connection before making connection and Verify that the OS Version is 6.0 and above
    If ((!(Test-Connection -comp $computer -count 1 -quiet)) -Or ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt 6.0)) {
    Write-Warning "$computer is not accessible or The Operating System of the computer is not supported.`nClient: Vista and above`nServer: Windows 2008 and above."
    }
    else {
    Invoke-Command -ComputerName $computer -ScriptBlock $scriptBlock -ArgumentList $username
    }
    }


Advertisement