PowerShell small Script--Bulk Add user properties----Export Login time

Source: Internet
Author: User
Tags samaccountname

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.