Solved

Removing items from a PowerShell ArrayList

Posted on 2013-05-24
9
2,560 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
Comment Utility
Try..
$ADusers = $ADusers | Where {$_.LastName -ne "Usrey" -and $_.FirstName -ne "Test"}

Open in new window

0
 

Author Comment

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

Assisted Solution

by:Subsun
Subsun earned 260 total points
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Daniel Usrey
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you gentlemen for your help I have some ideas here on differnet ways to approach the final objective.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
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 …

772 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