# StaggerNTNames.ps1 # PowerShell script to export the sAMAccountNames of all enabled users # in the domain with passwords that expire into a series of CSV files. # # Copyright (c) 2018 Richard L. Mueller # Version 1.0 - November 23, 2018 # # ---------------------------------------------------------------------- # You have a royalty-free right to use, modify, reproduce, and # distribute this script file in any way you find useful, provided that # you agree that the copyright owner above has no warranty, obligations, # or liability for such use. # Specify the base CSV file name. # Files will be created in the current directory with names like # n.csv, where n is an integer incrementing from 1. $FileName = "Users" # Specify the number of CSV files to be created. $NumFiles = 5 # Retrieve the sAMAccountName of all enabled users with passwords that expire. $Users = Get-ADUser -Filter {(Enabled -eq $True) -And (PasswordNotRequired -eq $False) -And (PasswordNeverExpires -eq $False)} | Select sAMAccountName # If your users do not yet have passwords that expire, use the following for all enabled users. # $Users = Get-ADUser -Filter {Enabled -eq $True} | Select sAMAccountName # And, if you want to include disabled users, use the following. # $Users = Get-ADUser -Filter * | Select sAMAccountName # Determine the number of users retrieved. $NumUsers = $Users.Count "Total number of users: $NumUsers" # Calculate the maximum number of users per CSV file. $UsersPerCSV = [Math]::Ceiling($NumUsers / $NumFiles) # $i is the CSV file number, incrementing from 1. $i = 1 # $j is the number of users in the CSV file. $j = 0 # $m is the total number of users processed. $m = 0 ForEach ($User In $Users) { $j = $j + 1 $m = $m + 1 # Add the header line, defining the field "ID". If ($j -eq 1) {"ID" >> .\$FileName$i.csv} "$User" >> .\$FileName$i.csv If ($j -eq $UsersPerCSV) { "$FileName$i.csv has $j users" $i = $i + 1 $j = 0 } } # Process the last CSV file, if there are more users. If ($j -gt 0) { "$FileName$i.csv has $j users" } "Total number of users in the all $NumFiles CSV files: $m"