- Copy the script in a notepad and save it as 'AD_DisplayNames_to_AccountNames.ps1'
- Use the same format for csv and add your DisplayNames and save it in the same folder as your script.
- Both script and csv are saved in the desktop
- Open powershell as Administrator (or ) Windows Powershell ISE that comes along with debugging. Using Powershell ISE can skip 5 thru 8 and open your script and hit F5
- In the powershell Navigate to your desktop (where script and csv are present)
- Set-ExecutionPolicy RemoteSigned
- Click Yes
- Now run the script by typing ./AD_DisplayNames_to_AccountNames.ps1
- check for newly added txt and csv files for other properties in AD for the given displaynames
Powershell Script:
# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory$Sam = @()
$names = Import-csv "C:\Users\\Desktop\AD_input_DisplayNames.csv"
foreach ($name in $names)
{
$dispName = $name.displayname
$sam += get-aduser -filter {displayname -like $dispName } -Property SamaccountName,DisplayName,office -Server ad3.xyz.com| Select DisplayName,SamaccountName,office
}
write-host $sam
#output to a text file format
$Sam | out-file 'C:\Users\\Desktop\AD_AccountNames.txt'
#output to a csv file format
$Sam | Export-Csv -Path 'C:\Users\Desktop\AD_AccountNames.csv' -NoTypeInformation
Input: (AD_input_DisplayNames.csv)
displayname
DisplayName1
DisplayName2
Output: (AD_AccountNames.csv)
"DisplayName","SamaccountName","office"
"DisplayName1","accountname1","Office Name "
"DisplayName2","accountname2","Office Name "
No comments:
Post a Comment