Looking to create Atera Script to list Local Users and another to remove listed Local Users

pzafian
pzafian Member Posts: 2
edited December 2023 in Scripts

Hi. I want to find someone who can help me develop a "relatively" automated solution to list all the local windows users on a group of Windows 10 PCs. Ideally this script could be run using an IT Automation assigned to the group of PCs and the results would be saved on the PC running the Atera Portal.

Then I'd like to develop a script that would remove a list of local users from a specific PC.

So far, I have figured out I can use the PS cmdlets Get-LocalUser and Remove-LocalUser and run these from the Manage/Powershell menu, but that still has me going to each PC and the output is displayed on the screen. Would like to automate this further and have output generate in a more useable fashion.

Please reply if you are interested.

Tagged:

Comments

  • kim
    kim Member Posts: 113 ✭✭✭

    HI!

    I've seen some scripts that do this in the Atera Shared Script Library: https://app.atera.com/new/admin/scripts If you search for "Lists local users on PC" there will be a script that pops up that you can clone and use in your environment. Once you get it into your environment, you can run it on all or select machines. Totally up to you on your scope. Likewise after you clone it, you can add the Remove local users command and run it as an administrator to complete.

    I'm not 100% sure how to have it say export at as report locally, meaning run this on x machines then output to a single file or report in my environment. May you could create reporting folder under the admin account on each machine and have the report saved there, then at least you would have known location on where to grab the reports. Just an idea, but that would be a cool feature to have.

    Sincerely,

    Kim


  • mjones
    mjones Member Posts: 183 ✭✭✭✭

    I just submitted a script that might do what you are looking for with some minor tweaking.
    Just be careful since it's a wildcard search and will purge the user and the profile folder.

    Enummerate the list of users
    $removeUser = Get-LocalUser -Name "{[User_Search_Name]}" | Select-Object -ExpandProperty Name
    
    Remove all of the users in the array
    foreach ($currUser in $removeUser)
    {
    Write-Output $currUser } try {
    Remove-LocalUser -Name $currUser -erroraction Stop
    } catch {
    Write-Output "Unable to locate any users."
    Exit 0
    }

    # Get the path to the user's profile folder
    $profilePath = "C:\Users\$currUser"

    # Check if the profile folder exists
    if (Test-Path $profilePath) {
    # Remove the profile folder
    Remove-Item $profilePath -Recurse -Force
    Write-Output "Profile folder for $currUser has been removed."
    } else {
    Write-Output "Profile folder for $currUser does not exist."
    }

  • nina
    nina Administrator Posts: 428 admin

    @MJones - legend! 👑

  • deanna.birdsall
    deanna.birdsall Member Posts: 1
    edited November 2023

    I have been working on something very similar.

    After creating your scripts, set them up through IT Automation. You will be able to download the results.

    I have a powershell script that lists all local users, status and group membership.
    Based on those results, I have a cmd line script that removes specified users, and adds others.

    I am new at posting here and haven't quite figured out how to post my scripts …

  • mjones
    mjones Member Posts: 183 ✭✭✭✭

    Excited to see what you have.

    You can add a "Code Block" from the paragraph button on the left
    Paragraph > Quotes > Code Block