PowerShell show null results

Greetings all,

Scenario: I've been given a list of users who are (apparently) in Active Directory. I've been asked to get their username and e-mail address from AD. The code used is as follows:

$users = Get-Content C:\Path\to\users.txt

**Content of users.txt is:

**blogs, joe

**smith, generic

**user, not exist

**davies, generic

foreach ($user in $users) { Get-ADUser -ldapfilter "(displayname=$user)" -Property * |
Select-Object -Property displayname,samaccountname,mail }

The above works great and outputs the following:

displayname           samaccountname      mail

--------------           -------------------      -----

blogs, joe                 blogsj                       joe.blogs@domain.com

smith, generic          smithg                     generic.smith@domain.com

davies, generic         daviesg                    generic.davies@domain.com

Fine and dandy but is it at all possible to display users who cannot be found so the output can be (or similar to) the below:

nameinfile         displayname           samaccountname      mail

-----------          --------------           -------------------      -----

blogs, joe          blogs, joe                blogsj                         joe.blogs@domain.com

smith, generic    smith, generic        smithg                       generic.smith@domain.com

user, not exist    <>                          <>                             <>

davies, generic  davies, generic       daviesg                      generic.davies@domain.com

(Granted I probably don't need 'displayname' here).

Many thanks in advance.

~IceWolf762

February 17th, 2015 12:26pm

Yes just build a custom object.

foreach ($user in $users){
     $p=@{
          DisplayName=$user
          SamAccountName=$null
          Mail=$null
      }
     if($user=Get-ADUser -ldapfilter "(displayname=$user)" -Property samaccountname,mail){
         $p.mail=$user.mail
         $p.Samaccountname=$user.samaccountanme
     }

     New-Object PsObject -Property $p 
}

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 12:54pm

Hi IW,

if you really want that "<>" where the user was not found, then you'll need to do a bit more than one line:

$Users = Get-Content "C:\Path\to\users.txt"
$Results = @()

foreach ($User in $Users)
{
	$tempUser = $null
	$tempUser = Get-ADUser -ldapfilter "(displayname=$User)" -Properties DisplayName, Mail
	if ($TempUser)
	{
		$Results += $tempUser | Select-Object @{ n = "NameInFile"; e = { $User } }, DisplayName, SamAccountName, Mail
	}
	else
	{
		$Results += "<>" | Select-Object @{ n = "NameInFile"; e = { $User } }, @{ n = "DisplayName"; e = { $_ } }, @{ n = "SamAccountName"; e = { $_ } }, @{ n = "Mail"; e = { $_ } }
	}
}

$Results

If all you care about is having them listed (even if the entries are empty), you can simply add the name from file like to your original query this:

$users = Get-Content C:\Path\to\users.txt
foreach ($user in $users) {	Get-ADUser -ldapfilter "(displayname=$user)" -Property DisplayName, Mail | Select-Object -Property @{ n = "NameInFile"; e = { $User } }, displayname, samaccountname, mail }

Cheers,
Fred

February 17th, 2015 12:56pm

Forgive Fred. He is still a diehard PowerShell V1 user. ;)

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 12:58pm

Thanks jrv, worked like a charm. Apologies for getting back to you late!
April 29th, 2015 7:33am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics