Solved

Removing items from a PowerShell ArrayList

Posted on 2013-05-24
9
3,148 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
[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
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 70

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
 

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 40

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

628 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