×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Powershell script to delete a couple of LOCAL users from remote computers

Powershell script to delete a couple of LOCAL users from remote computers

Powershell script to delete a couple of LOCAL users from remote computers

(OP)
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()}
}


}

RE: Powershell script to delete a couple of LOCAL users from remote computers

(OP)
I have the script now. but have another requirement. I need to export (append) data to two csv files.

1. If the hostname is not found I need to append the value of $hostname to a csv file named "nohost.csv"

2. Also I would need to append the value of $hostname from which user are successfully deleted, to "successfull.csv".

May I know how to use the "export-csv –append –path" command with each iteration of the for loop?

clear
# Path of the CSV file
$hostdetail = Import-CSV C:\Users\jossy.jacob\Desktop\Test\hosts.csv

$scriptBlock = {
Remove-LocalUser -Name $args[0]
}

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

#Test network connection before making connection and Verify that the OS Version is 6.0 and above
If ((!(Test-Connection -comp $hostname -count 1 -quiet)) -Or ((Get-WmiObject -ComputerName $hostname Win32_OperatingSystem -ea stop).Version -lt 6.0)) {
Write-Warning "$hostname 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 $hostname -ScriptBlock $scriptBlock -ArgumentList $username
}
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close