Export the value of a Variable to .CVS

Friends, Can you please help me with this?
I cannot figure out how to export the values of a Variable to .CVS
The Script imports a list of supposed SamAccountNames, butI I want to Export the ones that are not good.
The script works except for the Exporting.

foreach ($User in $ADUsers) 
.
.

If (Get-ADUser -F {SamAccountName -eq $Input}) 
{
    Do Something
}
     Else
        { 
         $Input | export-csv C:\FolderA\NotFound.csv  -NoTypeInformation -append   
        }

The Resulting Exported Excel File looks like this

Length
2
3
8
11
7
14

But I want something like this because these are the values of $Input

BJ
B.J
Joe.Jonz
Vlad, Putin
Xyz.Abc
JokerInTheDeck

And by the way, how can I delete that Export File at the beginning of the Script?

Thank you in advance for your help on this.

You do not share how you populate the variable $ADUsers. If it’s not just a simple list of plain text strings you may specify the property of the objects you may have stored in $ADUsers.
Then you define the variable $User as your loop variable but you use $Input inside your loop.

Olaf, Thank you for your reply.
Here’s the beginning of the script:

Import-Module activedirectory 
$ADUsers = Import-csv C:\FolderA\Filex.csv 
foreach ($User in $ADUsers) 
{
	$Input	= $User.Namez 
       if (Get-ADUser -F {SamAccountName -eq $Input }) 
}
       Else
           { 
         $Input | export-csv C:\FolderA\NotFound.csv  -NoTypeInformation -append   
           }

And the Import File is a list of supposed SamAccountName (s) with the Heading: Namez

Namez
Sam.Hill
BF.GoodRich
BJ
B.J
Joe.Jonz
Vlad,Putin
XYZ.ABC
JokerInTheDeck
Simon.Cox2
0725191F-1Z56-41EF-9126-L51979542GJ9

Instead of a condition you should use a try catch block:

$ADUserList = Import-csv -Path 'C:\FolderA\Filex.csv'
$NonExistingADUserList = 
foreach ($ADUser in $ADUserList) {
    try {
        Get-ADUser -Identity $($ADUser.Namez) -ErrorAction Stop | Out-Null
    }
    catch {
        $ADUser
    }
}
$NonExistingADUserList |
Export-Csv -Path 'C:\FolderA\NonExistingADUserList.csv' -NoTypeInformation

And BTW: in the CSV data you share you have comma in one item. If that’s the case your real input data you should provide a delimiter for Import-Csv when there are really sAMAccountNames with commata in it. :wink:

Olaf, thank you for your reply. I have lots of studying to do because I have never used TRY or CATCH. But I think I get it. I tried your script, it works great.

But unfortunately, I am stuck with using the -Filter instead of -Identity. Because I did not tell you the whole story, I did not think it was necessary.

I only want to export if it does NOT pass any Five, (5) of these tests.

If it is NOT any of these: Good SamAccountname or CN, or Name, or Mail, or PesonGuid…
Here is some pseudo-code that will show you what logic I have in my script

By the way, my logic and script work alright, except for the Export Statement.

If (Get-ADUser -F {SamAccountName -eq $Input}) 
        Do something
Else
     If (Get-ADUser -F {Name -eq $Input }) 
        Do something
     Else
          If (Get-ADUser -F {CN -eq $Input })
             Do Something 
          Else
               If (Get-ADUser -F {mail -eq $Input })
                    Do Something 
               Else
                    If (Get-ADUser -F {PersonGuid -eq $Input })
                         Do Something
                         Else
                              Export Right Here

So, just start with the help. :wink:

You should urgently consider using another approach. Instead of querying every single name 5 times directly from your AD you should read ALL possible accounts from your AD in advance, save it in a variable and query only this list. :wink:

1 Like

Olaf, Thank you, but “…read ALL possible accounts…” is just not the approach I need.
The idea is to see if each item in the Import CSV file can match an AD Record.
Each item in the Import CSV file might be:
SamAccountName
CN
Name
Email Address
PersonGuid

If an item in the Import CSV file is any of the above the script must Export to one .CSV.
If the Import CSV file input is none of the above I am trying to export to a totally different .CSV file to include the VALUE of the input. This would essentially say “This one did not find an AD Record!”

These managers request changes containing several hundred names. The managers are lazy. Sometimes the list of names are good, sometimes they are not good. Yes, yes, I know, in a perfect world they would supply good names. Then it is our job to make these changes, everyone else uses GUI. I am one of the first at my shop to try to develop some PowerShell tools. So if I am to use Powershell for hundreds and hundreds of changes, I MUST KNOW WHICH NAMES ON THE LIST ARE GOOD AND THEN GET THE SamAccountName. And I MUST KNOW WHICH ONES ARE BAD TOO.

Are you sure about this? :wink:

Using your approach you query each individual element of your CSV against your AD and this 5 times. That is time consuming and stressful for your AD.

A faster and way less stressful way would be to query the proper OU from your AD and save the accounts with all needed properties in a variable/array. Now you can run your checks against this variable/array and export all CSV elements you cannot find in the variable/array to another CSV. Why would that not be the approach you need?

There’s no need to scream at me. I did understand this completely the first time.

Something like this should be enough:

$SearchBase = 'OU=Users,OU=Sites,OU=Germany,OU=Europe,DC=Compnay,DC=de'
$ADUserList = Get-ADUser -Filter * -SearchBase $SearchBase -Properties Mail, CN

$InputUserList = Import-Csv -Path 'C:\FolderA\Filex.csv'
$NonExistingADUserList =
foreach ($item in $InputUserList ) {
    if (
        $item.Namez -notin $ADUserList.sAMAccountName -and
        $item.Namez -notin $ADUserList.ObjectGUID -and
        $item.Namez -notin $ADUserList.Name -and
        $item.Namez -notin $ADUserList.Mail -and
        $item.Namez -notin $ADUserList.CN
    ) {
        $item
    }
}

$NonExistingADUserList |
Export-Csv -Path 'C:\FolderA\NonExistingADUserList.csv' -NoTypeInformation

BTW: The property PersonGUID seems not to be a default attribute of an AD. So I assumed you meant the ObjectGUID

1 Like

Olaf, Thank you for your reply. I can see at a glance that your approach is compact, elegant and likey works very well. I can only dream about writing stuff like that. I truly mean to study it because I know from experience that when I understand something like that I will be able to do much more.
( I do need PersonGuid).
I have to get to work, I will study your words very carefully. Yesterday I worked over 12 hours.
By the way, I saved my co-workes hours and hours of woking witht the GUI with this simple thing:

Import-Csv -Path "C:\FolderA\xxx.csv" |            
foreach {  
      Set-ADUser -Identity $_.namez -Manager $_.manager 
}

But I had to get the good SamAccountNames from a list the boss gave me. There were hundreds. Some were good, some were bad. For some, all they had was an email address.

Thank you, your efforts are appreciated.

If you have something like PersonGUID in your AD you have to adapt the code accordingly.