Adding Users and Disabling Accounts

I have created a script to add users to a Domain through a txt file. The txt file is formated like:
First Name,Last Name,Age,OU

the Users are added fine, but I want to disable all accounts with an age over 75. the age is stored in the Desciption ($temp[2]).

This here is the script I have so far.

foreach ($User in Get-Content -Path C:\Users\Administrator\Desktop\moreNames.txt) {

$array = 'Acct', 'Mgmt', 'Production', 'Sales', 'Admin (Management Network)'
$array = $array | Get-Random
$temp = $User.Split(",")
$first_name = $temp[0]
$last_name = $temp[1]
$name = $temp[0] + $temp[1]
$email = $first_name[0] + $last_name.toLower() + ""
$samname = $first_name[0] + $last_name
$password = ConvertTo-SecureString "Welcome32$" -AsPlainText -Force

New-ADUser -Name $name -SamAccountName $samname -UserPrincipalName $email -GivenName $temp[0] -Surname $temp[1] -Description $temp[2] -Enabled $true -ChangepasswordAtLogon $true -Path "OU=$array, DC=balrok, DC=edu" -AccountPassword $password

$ageLimit = 75

if($temp[2] -ge $ageLimit)
	{Disable-ADAccount  -Identity $User}


This is my first post here let me know if I should be doing something different, or posting in a different area. Thanks

Hello Tiberious and welcome…

Please format your examples as code, it makes it easier for us to read and test ourselves. Have a look here :point_right: Guide to posting code.
You can go back and edit your post rather than repost the code.

I’ll not go deeper before the formatting is fixed, but I have a couple of initial thoughts…
If there’s a hard age limit for accounts, would it not be better to just check the age field before creating the account - if the age is above 75 just skip to the next user?

If your moreNames.txt file actually is formatted as you describe you’re probably better of using Import-Csv rather than Get-Content. Powershell handles CSV content much more conveniently than it does pure text.

Finally, I’d really recommend you to use more descriptive variable names in your scripts.As the scripts get longer and more complex you want to know what the variables contain rather than what type they are.

I still want the account to be created if the user is over 75 i just want the account to be disabled
I also tried to use a csv file before but found using attributes was much harder for me so I used a txt file with Get-Content


You might want to think about splatting, I am working on a similar script here: New AD User automation - Make functions or not
It just makes it a bit tidier and the line where New-AdUser is will be shorter. I use a csv which
@laage recommended.

You could even do an if statement before you create the user and set the Enabled parameter to false or true depending on the age, eg.

if($temp[2] -ge $ageLimit) {

   $IsEnabled = $false 

} else {

    $IsEnabled = $true


new-aduser .... -enabled $IsEnabled

Just an idea.

If you want to do it your way, then you need to to do something like this but you need to pass the User object to disable-adaccount, I haven’t tested this though

$ADuser = New-Aduser .. -passthru

$ageLimit = 75

if($temp[2] -ge $ageLimit)
	{Disable-ADAccount  -Identity $ADuser}