I am in need to disable about 250 local user accounts based on input in a text file or CSV-file and then export the result into a CSV file.
I have searched the net quiet a bit but I'm unable to find anything i can tailor togheter. Here is what I've tried;
This is one I tried:
$Workdir = 'C:\scripts\'
$Output = $Workdir + 'Disabled-UserReport.csv'
$InputFile = $Workdir + 'Users_To_Disable.csv'
$Servers = 'LOCALHOST'
Import-CSV $InputFile | ForEach-Object {
$Server = $_ ([ADSI]"WinNT://$Server").Children | ? {$_.SchemaClassName -eq 'user'} | % { $User.UserFlags[0] = $User.UserFlags[0] -bor 0x2 $User.SetInfo() }
}| Export-CSV -Encode UTF8 -Path C:\scripts\Disabled-Users.csv
This is try two:
$Servers = 'LOCALHOST'
$Username = Import-CSV "C:\scripts\Users_To_Disable.csv"
$Username | FOREACH {
$Server = $_
$objGroup = [ADSI]("WinNT://$env:ComputerName/User")
$objGroup.PSBase.Invoke("Add",$objUser.PSBase.Path)
$User.description = $description
<#$User.userflags = $DisableUser#>
$User.setinfo()
} | Export-CSV -Encode UTF8 -Path C:\scripts\Disabled-Users.csv
I know there is something very wrong with both scripts....and as you can see I'm a noob learning PS whenever I get the time :) It would be great if it works in PS2. But required to work with PS4.
Any help is appreciated!
Yes I finally managed last week.... Thank you! Code if some other newbies want. It disabled accounts based on inputs from a textfile and also sets the accounts to Password Never Expire and Password Cannot Be Changed;
$EnableUser = 512
$DisableUser = 2
$PasswordNotExpire = 65536
$PasswordCantChange = 64
$users = Get-Content -path "C:\Users_To_Disable.txt"
$computer = $env:COMPUTERNAME
Foreach($user in $users){ $user = [ADSI]"WinNT://$computer/$user"
$user.userflags = $DisableUser+$PasswordNotExpire+$PasswordCantChange
#$user.Userflags = $EnableUser+$PasswordNotExpire+$PasswordCantChange
$user.setinfo()
}