If you are using Posh v3 or higher you can just use the MS commandlets. Try
this:
$AllCustomizedObjects = @()
ForEach ($User in Get-ADUser -LDAPFilter "(!(lastname=\00))")
{
$CustomizedObject = $User
$CustomizedObject | Add-Member -MemberType NoteProperty -Name Matched
-Value $false
$AllCustomizedUsers += $CustomizedObject
}
It will give you all users where lastname is blank. It looks like that is what
you were asking for. If you want all those other attributes (you aren’t using
them in the snippet you provided) you can use a select statement on $user
within the loop to dump that data into an array.
Matt
From: [email protected] [mailto:[email protected]] On
Behalf Of Michael Leone
Sent: Tuesday, August 25, 2015 10:07 AM
To: [email protected]
Subject: [powershell] Add-Member question
So I have a list of AD users, that I created using Quest cmdlets (Get-ADUser
..). I need to add a new NoteProperty to each object (I want to add a boolean
member, to indicate that this object meets certain criteria, determined later
in the script). And I seem to be confused. (I know, what else is new).
Do I need to:
Do a ForEach-Object loop through all the returned users
Create a new CustomObject
Set it to the value of the user
Add-Member boolean field to the custom object
save custom object into new list?
$AllActiveUsers = Get-QADUser <OneUser> -Enabled -SizeLimit 0
-IncludedProperties employeeID, departmentNumber| Select
givenName,LastName,DisplayName,Email,sAMAccountName,employeeID,departmentNumber|
Where-Object {$_.LastName -ne $null} | sort LastName,givenName
$AllCustomizedObjects = @()
ForEach ($User in $AllActiveUsers)
{
$CustomizedObject = $User
$CustomizedObject | Add-Member -MemberType NoteProperty -Name
Matched -Value $false
$AllCustomizedUsers += $CustomizedObject
}
That works, but is there a better way?
I could come at it from the other direction; get my list of all users; when I
find one that matches the criteria later, at that point create a new object
with my boolean field; save all those objects; use those customized objects
later in output. That would double up memory use, but possibly shorten
execution time, since I wouldn't have to loop through all users first, adding
an object. Instead only adding objects when I find a match (which should only
be about 20% of the users, I am expecting).
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be
used for urgent or sensitive issues
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1