Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JamesCalleja/cc3f24628cbe1925931536b95e2f03e3 to your computer and use it in GitHub Desktop.

Select an option

Save JamesCalleja/cc3f24628cbe1925931536b95e2f03e3 to your computer and use it in GitHub Desktop.
Set logon hours for AD users across the forest
#########################################################
#Writen by James Calleja
#Date 22/06/2016
#########################################################
# create a byte array for the M-F 8:00 am to 8 pm logon hours
$hours = New-Object byte[] 21
$hours[0] = 0;#sunday 00:00 - 08:00
$hours[1] = 0;# 08:00 - 16:00
$hours[2] = 0;# 16:00 - 00:00
$hours[3] = 0;#monday 00:00 - 08:00
$hours[4] = 255;# 08:00 - 16:00
$hours[5] = 15;# 16:00 - 00:00
$hours[6] = 0;#tuesday 00:00 - 08:00
$hours[7] = 255;# 08:00 - 16:00
$hours[8] = 15;# 16:00 - 00:00
$hours[9] = 0;#wednesday 00:00 - 08:00
$hours[10] = 255;# 08:00 - 16:00
$hours[11] = 15;# 16:00 - 00:00
$hours[12] = 0;#Thursday 00:00 - 08:00
$hours[13] = 255;# 08:00 - 16:00
$hours[14] = 15;# 16:00 - 00:00
$hours[15] = 0;#Friday 00:00 - 08:00
$hours[16] = 255;# 08:00 - 16:00
$hours[17] = 15;# 16:00 - 00:00
$hours[18] = 0;#Saturday 00:00 - 08:00
$hours[19] = 0;# 08:00 - 16:00
$hours[20] = 0;# 16:00 - 00:00
# create a hashtable to update the logon hours and a description
$replaceHashTable = New-Object HashTable
$replaceHashTable.Add("logonHours", $hours)
$replaceHashTable.Add("description", "Monday through Friday from 08:00 to 20:00")
# set the value of the logonHours and description attributes
#Set-ADUser $_.name -Replace $replaceHashTable
(get-addomain).childdomains | %{(Get-ADDomainController -Discover -Domain $_).hostname} | %{Get-ADuser -Filter * -Server $_ -properties * | ?{$_ -like '*test*'}}|% {Set-ADUser $_ -Replace $replaceHashTable}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment