Solved

Removing items from a PowerShell ArrayList

Posted on 2013-05-24
9
2,675 Views
Last Modified: 2013-05-28
I have been assigned a task to verify current employees, using an Excel spreadsheet provided by HR, against Active Directory accounts. The list provided by HR only contained first and last names.  I was quickly able to identify about 40 percent of the list using a quick powershell script. I imported the csv file of users from HR as an array and I created a second array using get-qaduser command. Then I did a compare object to identify the users comparing first and last names. Then I was able to separate the users found in AD, the ones who were not and the user who only exist in AD. At this point I still had way too many users to have to verify manually so I am working on doing other comparisons of the users whom were not found in AD on the initial comparison. What I want to do to make the second comparison easier is to remove the confirmed employees from the array that was created by the query against AD. After digging I found it would be very difficult unless I converted the array to a .Net object arraylist. So, I did that and then I spent a couple of days trying to figure out how to remove users from the arraylist and I am still running into problems with that. I have commands that do not give an error and PowerGUI says that script was executed however I can turn right around and run the select command that will show the user is still part of the arraylist.

PS C:\scripts> $ADusers.Remove | Where {$_.LastName -eq "Usrey" -and $_.FirstName -eq "Test"}

PS C:\scripts> $ADusers | select * | Where {$_.LastName -eq "Usrey" -and $_.FirstName -eq "Test"}

LastName                                                  FirstName                                                
--------                                                  ---------                                                
Usrey                                                     Test        
This is another remove command that does not give an error however it does not remove the desired users either.
$Except2.Remove(($Except2 | Where {$_.SideIndicator -eq "=="}))
0
Comment
Question by:Daniel Usrey
9 Comments
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 260 total points
ID: 39194369
Try..
$ADusers = $ADusers | Where {$_.LastName -ne "Usrey" -and $_.FirstName -ne "Test"}

Open in new window

0
 

Author Comment

by:Daniel Usrey
ID: 39194383
Thanks Subsun that accually did remove the item from the array.
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 260 total points
ID: 39194440
Remove won't work because the array object doesn't contain that method.

$ADusers | Get-Member -membertype method -Force will give you the possible methods
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 20 total points
ID: 39194450
It does not "remove", it generates a new array and replaces the old one. That is the way PS works, but is highly inefficient if you remove one element only.

Because PS always creates new objects if you filter in a pipe, I cannot think of a method to correctly use any Remove method of ArrayList.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:Daniel Usrey
ID: 39194606
Thank you for the responses. I was wondering if the reason $ADusers.remove was not working is because of the way the arraylist was populated.

$ADU = Get-QADUser * -SizeLimit 0 | select -property lastname,firstname | Where-Object {$_.firstname -ne $null -and $_.lastname -ne $null}
$ADusers = New-Object System.Collections.ArrayList
$ADusers.AddRange($ADU)
0
 

Author Comment

by:Daniel Usrey
ID: 39194688
After more poking around with this I was wonding what happened to the arraylist. Subsun your method definitely removed the item from the array list howerver it converted it back from an arraylist to a normal array. I guess I can work with the normal array and try to remove the the items using a loop. But the reason I was using the arraylist was to take advantage fo the .remove option. Unfortunately, most of the documentation that I found only showed using the index of the arraylist items to remove it from the arraylist.
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 260 total points
ID: 39194808
IMO, Using Where-Object to filter the data is a quick and easy option. If you want to use the Remove method then you need to find the index of your value first and then remove it which makes it more complex..
0
 
LVL 39

Accepted Solution

by:
footech earned 220 total points
ID: 39194927
Granted, I really don't use arraylists, but I have found that the method like below does allow for removing the items.
$a = New-Object System.Collections.Arraylist(,(Get-ADUser -filter *))
foreach ($user in $($a)) {if ($user.givenname -eq "bob"){$a.Remove($user)}}
                  ^^^^^
this notation is important to avoid the error, "Collection was modified; enumeration operation may not execute".

Open in new window


Honestly, my preferred approach would be to take the output from Compare-Object and filter it into new variables (arrays) as desired.
What I want to do to make the second comparison easier is to remove the confirmed employees from the array that was created by the query against AD.
This should be very simple!  Have you tried something like this?
$unMatchedAD = Compare-Object $list $ADU -property surname,givenname -PassThru | Where { $_.SideIndicator -eq "=>" }

Open in new window

0
 

Author Closing Comment

by:Daniel Usrey
ID: 39201212
Thank you gentlemen for your help I have some ideas here on differnet ways to approach the final objective.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now