Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Removing items from a PowerShell ArrayList

Posted on 2013-05-24
9
Medium Priority
?
3,525 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 780 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 780 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 60 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 780 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 41

Accepted Solution

by:
footech earned 660 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

783 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