Solved

Removing items from a PowerShell ArrayList

Posted on 2013-05-24
9
2,988 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Suggested Solutions

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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 …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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