Requirement 1: All employees in a company are missing the mail attribute and need to be added in bulk. For example, user Chenyy add message properties [email protected]
Pilot out (name only) Alternate:
Get-aduser-filter * -properties * | select name | Export-csv C:\test.csv
Use the Where condition to filter the SYSTEM account
Get-aduser-filter * -properties * |where {$_. Userprincipalname-ne $null} | Select Name | Export-csv C:\test.csv
* Then open test.csv add a table header, named user (this is not filtered)
Import and write a loop:
$file = Import-csv C:\test.csvforeach ($User in $file) {$username = $User. Data # $User represents this line, $User. Data represents this line of information, gets the user name Chenyy $str = "@xxxx. com" $mail = $username + $str #合并字符串 [email protected] set-aduser -indentity $User. Data-emai Laddress $mail #用set-aduser insert Mail Property}
Requirement 2: Gets the user's most recent logon time. (table header is user)
$Line = $Contents. Length #获取行数 (People 0 start)
for ($i =0; $i-lt $Line; $i + +) {$User = $Contents. user[$i] #表头为User
Write-output "The User is $User" #打印当前用户
#最后登陆时间戳 $Stamp =get-aduser-identity $User. data-properties * | Select-object name,distinguishedname,@{name= "Stamp"; Expression={[system.datetime]::fromfiletime ($_.lastlogontimestamp)}} #获取当前用户最后登陆时间
Write-output $Stamp >> c:\result.csv #导出为csv
}
=============================================================================================================== =========================================================================================
PowerShell bulk Modify user's UPN suffix applies to products: Windows Server activedirectory queries the user for an empty UPN in AD
where {$_.UserPrincipalName -eq $null} | Select-Object name,SamAccountName,UserPrincipalName
Set the UPN suffix
where {$_.UserPrincipalName -eq $null} | Select-Object name,SamAccountName,UserPrincipalName | foreach {Set-ADUser -Identity $_.name -UserPrincipalName ($_.SamAccountName+"@contoso.com")}
Query results
PS c:\users\administrator> get-aduser-filter *-properties * |where {$_. userPrincipalName-eq $null} | Select-object name,samaccountname,userprincipalnamename sAMAccountName userPrincipalName -----------------------------------Guest Guest KRBTGT krbtgt Mailuser2 Mailuser2 Mailuser3 Mailuser3 Mailuser4 Mailuser4 Mailus Er5 Mailuser5 Mailuser6 Mailuser6 Mailuser7 Mailuser7 Mailuser8 Mailuser8
Set results
Get-aduser-filter *-properties * | where {$_. userPrincipalName-Ne $null} | Select-object Name,samaccountname,userprincipalnamename SAMACCOUNTN Ame userPrincipalName---- -------------- ----------------- Administrator Administrator [Email protected] Guest Guest [E Mail protected] Krbtgt krbtgt [email protected] Exchange Online-applicationaccouNT $33100 0-k0sah4ncdj2k
PowerShell small Script--Bulk Add user properties----Export Login time