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?
Any help highly appreciated...
1. Remove-LocalUser -Name $username}
2. Invoke-Command -ComputerName $computer -ScriptBlock {$username.Delete()}
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()}
}
}
Regarding the invoke command which one I should use?
Any help highly appreciated...
1. Remove-LocalUser -Name $username}
2. Invoke-Command -ComputerName $computer -ScriptBlock {$username.Delete()}
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()}
}
}