add logged in user to local administrators group powershell site:www.reddit.com code example

Example: add logged in user to local administrators group powershell site:www.reddit.com

$LocalhostSession= New-PSSession -ComputerName localhost -Credential $mycreds 
$Arguments = @($username)
$sb= {Param ([string]$username); $de = [ADSI]"WinNT://$env:computername/administrators,group"; $de.psbase.Invoke("Add",([ADSI]"WinNT://domain.net/$username").path)}
Invoke-Command -ComputerName $env:COMPUTERNAME -Session $LocalhostSession  -ArgumentList $Arguments -ScriptBlock $sb

Tags:

Misc Example