Need Help with Scripting Output!

Hi All,

I am new to PowerShell and I keep running into this problem. I have a script that will pull names from a list of users.csv (1723) and find all of the SamAccountNames and output them in a new list. However, when pulling up the new list it only shows 1428 of the users. When I look back at who’s missing I see that in AD the name will be Alex but in my list it shows Alexander. So I assume that PowerShell skips the names it cant find. Is there a way to have the list of names that cant be found to have “Null” or “NA” in the output list instead of leaving them out of the list entirely? My end goal is to input the manager of the user named in the list into AD for ALL users not just 1428.

Here is the script I’m using to pull SamAccountNames from AD:

Import-Module activedirectory
$displayname = @()
$names = get-content "c:\user.txt"
foreach ($name in $names) {

$displaynamedetails = Get-ADUser -filter { DisplayName -eq $name } -server "Domain name "| Select name,samAccountName

$displayname += $displaynamedetails


$displayname | Export-Csv "C:\Samaccountname.csv"

Here is the script I’m using to input the users manager with their SamAccountNames:

Import-Module ActiveDirectory
$Book2 = Import-csv c:\Scripts\Book2.csv
foreach ($User in $Book2)
Set-ADUser $User.SamAccountName -Manager $User.Newmanager 

Any help will be appreciated!

When you ask for information, you must use an exact match or a wild card match.
Since AD Names are unique, you need to pass in a exact list.
You can use a wildcard match, using the normal PowerShell comparison operators.


$displaynamedetails = Get-ADUser -filter { DisplayName -like "*$name*" } -server "Domain name " | 
Select name,samAccountName

As a rule when creating scripts, it best to do it one segment at a time to make sure you are getting what you’d expect before moving forward, then once you’ve done that, go back and make it concise / elegant.

Trying to do it all at once, just leads to unnecessary misconception / confusion / errors.

I tried this script but the output was blank? Am I missing something?

Have you tried the if else route ?

if true then display name,samAccountName

else $displayname += ‘NA’

I would spit the not found items to another list using try / catch, so happens i wrote this yesterday for a task

assumes you have a excel sheet with column1 line1 as below


$users = import-csv C:\temp\UN.csv

$results = foreach ($User in $Users)

get-aduser -Identity $user.samaccountname -Properties whencreated | select Samaccountname, whencreated}


$error1 = "User *$user.SamAccountName* was not found"

$error1 | out-file c:\temp\unerrors.txt -append

$results | Out-GridView


I think you’re running into the common issue we all experience with HR data.
Using displayname is not a very good idea to try and find network id’s for specific users.

I’d recommend looking for some other identifier in your data to get to a distinct identifier for each user to search on.

Without knowing your data source, usually, email is an ok thing to search upon, as it will always be a distinct value.
Otherwise, you’ll need a lot of logic around your code to identify when multiple ad accounts are returned from your query on displaynames