Powershell script to remove local users from remote computers

I would like to delete a couple of local users (NOT domain users) from remote computers. May I know whether the following code would work?

Regarding the invoke command which one I should use?

1. Remove-LocalUser -Name $username}
2. Invoke-Command -ComputerName $computer -ScriptBlock {$username.Delete()}

Any help highly appreciated...

clear
$hostdetail = Import-CSV C:\Users\jj\Desktop\Test\hosts.csv

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

#Test network connection and operating system version
 
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 {Remove-LocalUser -Name $username}
  # Invoke-Command -ComputerName $computer -ScriptBlock {$username.Delete()}
  }


}

Parents Reply Children
No Data