Function to run conditional on CSV and delete line if true

It's Friday and my brain is focusing more on the weekend than work, so I ask your help, oh PowerShell community:

I've been tasked with converting our old VB new user script to Powershell, the input of which is a CSV that HR sends us on a daily basis. On rare occasion they include someone who already exists as a user, and I'd like to write a function to check the CSV for that and remove that line in the CSV before continuing processing. So far I have the following:

Function Clear-Existing
    {
    param
        (
        [string]$path
        )
        $csv_input = import-csv -Delimiter ',' $path -Header @("EmpID","LastName","FirstName","Title","Dept","Status") | % ( $_.EmpID 
        foreach ($entry in $csv_input)
            {
            if (Check-ADUser $entry.EmpID -eq $true)
                {
                write-host "user $entry already exists!"
                }
            }
    }

But being said Friday, I can't figure out how to remove the "offending" line from the CSV and save it.

March 27th, 2015 12:23pm

Why not just process the csv file, and before creating the new user, do a check to see if they exist?

if (Get-ADUser $csv.sAMAccountName)
{
  # USER EXISTS
}
else
{
  # USER DOES NOT EXIST
}

Free Windows Admin Tool Kit Click here and download it now
March 27th, 2015 12:35pm

The script calls on the CSV file later to do other things like add to appropriate groups and enable mailbox. I'd rather just remove the existing users at the start than check for errors in every section.
March 27th, 2015 12:37pm

It is actually very easy to do as you read in the CSV file.

$csv_input=import-csv $path -Header EmpID,LastName,FirstName,Title,Dept,Status) |
     where-Object{-not (Get-AdUser -filter "EmployeeId -eq '$($_.EmpID)'")}

Free Windows Admin Tool Kit Click here and download it now
March 27th, 2015 12:49pm

Try something like this, you may want to change the path on the Export-Csv file to verify first, so that way you don't overwrite your file
Function Clear-Existing
{
  param
  (
    [string]$path
  )
  $results = @()
  import-csv -Delimiter ',' $path -Header @("EmpID","LastName","FirstName","Title","Dept","Status") | ForEach {
    if (Check-ADUser $_.EmpID -eq $true)
    {
       write-host "user $entry already exists!"
    }
    else
    {
      $results += $_
    }
  }

  $results | Export-Csv $path
}

March 27th, 2015 12:53pm

That doesn't seem to be doing anything when I try running it.

Can you explain the logic behind it, please?

Free Windows Admin Tool Kit Click here and download it now
March 27th, 2015 2:41pm

Unfortunately pulling the CSV into an array means I'll have quotes in my CSV on export.
March 27th, 2015 2:41pm

Nevermind, figured that one out myself:

$results | % {$_ -replace '"',''} | Export-Csv $path -NoTypeInformation

Thanks!

Free Windows Admin Tool Kit Click here and download it now
March 27th, 2015 2:48pm

CSV files are supposed to have quotes.  It is required for any field containing a comma or a quote.

Filtering on load is the best way to accomplish your goal.  There seems to be no gain by creating a new CSV.

March 27th, 2015 3:41pm

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

Other recent topics Other recent topics