Set a random password to an Active Directory user
Set a random password to an Active Directory user

This script use the function GET-Temppassword to set a random password to a user account. You can customize this function with your own criteria

import-module activedirectory

Function GET-Temppassword() {
	$chars_min = [Char[]]"abcdefghijklmnopqrstuvwxyz"
	$chars_maj = [Char[]]"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	$chars_num = [Char[]]"0123456789"
	$x_min = 9
	$x_maj = 9
	$x_num = 9
	$x_tot = $x_min + $x_maj + $x_num 

	$TempPassword=	($chars_min | Get-Random -Count $x_min)+ `
					($chars_maj | Get-Random -Count $x_maj)+ `
					($chars_num | Get-Random -Count $x_num)
					
	$final_rand_password = ($TempPassword | Get-Random -Count $x_tot) -join ""
	return $final_rand_password
}

$DistinguishedName = "cn=youruseraccount,ou=users,dc=domain,dc=local"

$rand_password = GET-Temppassword
Set-ADAccountPassword $DistinguishedName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $rand_password -Force)

write-host "Username : $DistinguishedName / New password : $rand_password"

This script uses the powershell cmdlet Get-Random:

The Get-Random cmdlet gets a randomly selected number. If you submit a collection of objects to Get-Random, it gets one or more randomly selected objects from the collection.

Without parameters or input, a Get-Random command returns a randomly selected 32-bit unsigned integer between 0 (zero) and Int32.MaxValue (0x7FFFFFFF, 2,147,483,647).

You can use the parameters of Get-Random to specify a seed number, minimum and maximum values, and the number of objects returned from a submitted collection.

<>

My Powershell script categories

Set a random password to an Active Directory user

Leave a Reply

Your email address will not be published.