Solved

Import-CSV  and remove specified values

Posted on 2016-08-18
5
46 Views
Last Modified: 2016-09-12
I have a powershell script to export some account info from AD. I want to read this CSV file in and remove certain accounts. I can't do it dynamically as it's something a manager needs to specify. The CSV file output is below

"Email","FirstName","LastName","Department","whenCreated"
"User1@domain.com","UserF1","UserL1l","Cashiering","9/8/2006 11:33:53 AM"
"User2@domain.com","UserF2","UserL2l","Cashiering","9/13/2006 11:50:59 AM"
"User3@domain.com","UserF3","UserL3l","Cashiering","12/6/2012 2:51:43 PM"
"User4@domain.com","UserF4","UserL4l","Cashiering","12/6/2012 2:51:43 PM"




I'd like to be able to loop through a where clause and remove lines based on "Email address" I specify in the file. I forsee the need to add additonal accounts in the future so if I could use an array of some sort that would be best. I see a couple solutions online but not quite what I need. logic would be

Import CSV

If account is in the list below, remove the line from the CSV
"User1@domain.com"
"User4@domain.com"
"User9@domain.com"
"User10@domain.com"

Export-CSV Contents


any help is appreciated!
0
Comment
Question by:jbla9028
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 41761525
$ignore = @"
User1@domain.com
User4@domain.com
User9@domain.com
User10@domain.com
"@ -split "`r`n"

(Get-Content file.txt  | Select-String -NotMatch -SimpleMatch $ignore) | Out-File file2.txt

Open in new window

As I suppose comparing parts of the line with the email addresses is enough of a match to reliable exclude the unwanted lines. We can make the pattern more restrictive, or switch to your suggestion handling CSV data, if required.
0
 
LVL 1

Author Comment

by:jbla9028
ID: 41768168
Sorry for the delay, I was on vacation. I tried to add this statement but it's not removing the line from the CSV file. it's just outputing the same exact text file it read back in.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 41768316
There seems to be an EE issue with the backtick used, I suspect. Let's try something slightly different:
$ignore = 'User1@domain.com',
          'User4@domain.com',
          'User9@domain.com',
          'User10@domain.com'

(Get-Content file.txt  | Select-String -NotMatch -SimpleMatch $ignore) | Out-File file2.txt

Open in new window

0
 
LVL 1

Author Comment

by:jbla9028
ID: 41768902
]Thank you for the help again. So this time the user was removed but the CSV file structure was manipulated in a way that makes it unusable. For some reason records are now traversing multiple lines in the CSV file.


CSV Prior



CSV Post
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41769052
That has to be an issue with the app you use to read the file. Test with two entries only, and compare the files with a standard editor, then with the app.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question