Share via


Create multiple local user accounts with text file and disable them after a period of time time with powershell script.

Question

Friday, January 24, 2014 11:27 AM

Hi,

I need a script to create multiple local user accounts using a text file and then disable them after a period of time or disable them at a certain time. 

I managed to create multiple account with the text file, but I can't figure out the way to disable them.

That's what I got so far:

$computer = [System.Net.Dns]::GetHostName()
$text = "C:\Einu\Users.txt"
$user = import-csv -path $text

   foreach($strUser in $user)
    {
     $user = $struser.user
     $password = $struser.password
     $description =  $struser.description
     Clear-Host
     $ObjOU = [ADSI]"WinNT://$computer"
     $User = $objOU.Create("User", $user)
     $User.setpassword($password)
     $User.put("description",$description)
     $User.SetInfo()
     Clear-Host
     }

Einu

users.txt:

user, password, description
auser, asd, amees
buser, asd, bmees
cuser, asd, cmees

All replies (3)

Friday, January 24, 2014 1:18 PM ✅Answered

I don't understand if you need to disable them after 4 hs, or next Monday. 

This is how I would do it:

  • If you need to disable them in a certain amount of time, use the sleep command http://technet.microsoft.com/en-us/library/ee177002.aspx.
  • If you need to disable them next Monday, I would create 2 scripts, one to create them and another one to disable them, and I would configure the second one to run with the task scheduler whenever I want... 

Regards

EDIT: The other one is to create a recursive script, so it stays running until a moment in time, but I wouldn't recommend that.


Friday, January 24, 2014 1:22 PM ✅Answered

Have a look here...it has a Disable-LocalUserAccount function

http://berkenbile.wordpress.com/2013/04/26/manage-local-user-accounts-with-powershell/

For scheduling them you can create a scheduled task.....

Hope this helps

Knowledge is Power{Shell}.


Monday, January 27, 2014 1:47 AM ✅Answered

Hi,

Each local user has a property named UserFlags, this value indicates the user's status, we can disable user by setting this property's value.

As above suggestions, if you want to disable them at a certain time, we can create a schedule task to run the disable account script.

Please refer to the below articles for details about how to disable local user account:

Managing Local User Accounts with Windows PowerShell

http://www.petri.co.il/managing-local-user-accounts-with-powershell.htm#

Use PowerShell to Enable or Disable a Local User Account

http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/22/use-powershell-to-enable-or-disable-a-local-user-account.aspx

Hope this helps.

Regards,

Yan Li

Regards, Yan Li