localadmin.psm1

<#
    .NOTES
    =======================================
    Created by: Byran Schomburg
    Website: www.schomburg.dev
    =======================================
    .SYNOPSIS
    Adds or removes current logged in user as local Administrator
    .DESCRIPTION
    This Module adds or removes the current loggen in user to the local Administrators group.
    .EXAMPLE
    PS> add-localadmin
    .EXAMPLE
    PS> remove-localadmin
#>

function add-localadmin{
    if (!([Environment]::Is64BitProcess)) {
        if ([Environment]::Is64BitOperatingSystem) {
            Write-Output "Running 32 bit Powershell on 64 bit OS, restarting as 64 bit process..."
            $arguments = "-NoProfile -ExecutionPolicy ByPass -WindowStyle Hidden -File `"" + $myinvocation.mycommand.definition + "`""
            $path = (Join-Path $Env:SystemRoot -ChildPath "\sysnative\WindowsPowerShell\v1.0\powershell.exe")
            Start-Process $path -ArgumentList $arguments -wait
            Write-Output "finished x64 version of PS"
            Exit}
        else {Write-Output "Running 32 bit Powershell on 32 bit OS"}}
    $ErrorActionPreference = "Stop"
#
# get local admin group
#
    $LocalAdminGroup = (Get-LocalGroup -SID "S-1-5-32-544").name
#
# get the sam of the user who enrolled the computer to AAD
#
    $user = (Get-WMIObject -class Win32_ComputerSystem | select username).username
#
# Add user to local admin group
#
    Add-LocalGroupMember -Group $LocalAdminGroup -Member $user -ErrorAction SilentlyContinue
#
# Ask for logout
#
    do{
        $logout = read-host "Do you want to logout now? [Y/N]"
    }until(($logout -eq "Y") -or ($logout -eq "y") -or ($logout -eq "N") -or ($logout -eq "n"))
    if(($logout -eq "Y") -or ($logout -eq "y")){
        logoff
    }
    
}
function remove-localadmin{
    if (!([Environment]::Is64BitProcess)) {
        if ([Environment]::Is64BitOperatingSystem) {
            Write-Output "Running 32 bit Powershell on 64 bit OS, restarting as 64 bit process..."
            $arguments = "-NoProfile -ExecutionPolicy ByPass -WindowStyle Hidden -File `"" + $myinvocation.mycommand.definition + "`""
            $path = (Join-Path $Env:SystemRoot -ChildPath "\sysnative\WindowsPowerShell\v1.0\powershell.exe")
            Start-Process $path -ArgumentList $arguments -wait
            Write-Output "finished x64 version of PS"
            Exit}
    else {Write-Output "Running 32 bit Powershell on 32 bit OS"}}
#
# get local admin group
#
    $LocalAdminGroup = (Get-LocalGroup -SID "S-1-5-32-544").name
#
# get the sam of the user who enrolled the computer to AAD
#
    $user = (Get-WMIObject -class Win32_ComputerSystem | select username).username
#
# Add user to local admin group
#
    remove-LocalGroupMember -Group $LocalAdminGroup -Member $user -ErrorAction SilentlyContinue
}