I think so far that is working for the initial part. The next part i need to work on is a full change of every single employee ID changed from existing to updated one (without leading zeros). I have something that works pretty well so far but the where part seems to not work
ForEach ($employee in ($adplist | Where {$employee.Company -ne "Company A"})) {
Write-host $employee.'First Name' $employee.'Last Name' $employee.Company
That is just the start of it but the output shows me everyone on the list including everyone in Company A. I thought it should show me everyone excluding the people in Company A.
i think that’s because you’re not doing the operation in the where clause on the current piped in object , like so
#example 1
ForEach ($employee in ($adplist | Where {$_.Company -ne "Company A"})) {
Write-host $employee.'First Name' $employee.'Last Name' $employee.Company
#example 2
ForEach ($employee in ($adplist | Where Company -ne "Company A")) {
Write-host $employee.'First Name' $employee.'Last Name' $employee.Company
#example 3
ForEach ($employee in ($adplist | Where -Property Company -ne -Value "Company A")) {
Write-host $employee.'First Name' $employee.'Last Name' $employee.Company
The first one spit out the same as the code I posted and the last 2 gave errors not knowing what Company is or what Property conditions were.
What did work was changing it to Where {$_.Company -Match “Company B”}…
then it spit out only those in company B. It’s strange how this code used to work but updates or something changed how powershell reacts with AD overall and needs to be changed.
strange , the second and third syntax should work just fine in powershell v3+ , notice that there is no scriptblock (braces { }) in the second and third examples , the braces are from the foreach
with the -match operator you can do a lot more , check out help about_Regular_Expressions , also with the -like operator you can use wildcards
PS C:\> Get-ADUser -Properties * -Filter * -SearchBase $base | select name,company
name company
---- -------
User01 Company A
User02 Company B
User03 Company ABC
User04 Company BCA
User05 Company BC
User06 Company AB
User07 Company AC
User08 Company C
User09 Company B
User10 Company A
PS C:\> $us = Get-ADUser -Properties * -Filter * -SearchBase $base
PS C:\> ForEach ($u in ($us | Where -Property Company -eq -Value "Company A" )) { Write-host $u.Name $u.Company }
User01 Company A
User10 Company A
PS C:\> ForEach ($u in ($us | Where -Property Company -ne -Value "Company A" )) { Write-host $u.Name $u.Company }
User02 Company B
User03 Company ABC
User04 Company BCA
User05 Company BC
User06 Company AB
User07 Company AC
User08 Company C
User09 Company B
PS C:\> ForEach ($u in ($us | Where Company -ne "Company B" )) { Write-host $u.Name $u.Company }
User01 Company A
User03 Company ABC
User04 Company BCA
User05 Company BC
User06 Company AB
User07 Company AC
User08 Company C
User10 Company A
PS C:\> ForEach ($u in ($us | Where Company -eq "Company B" )) { Write-host $u.Name $u.Company }
User02 Company B
User09 Company B
PS C:\>
So again I wanted to thank everyone for helping me through this PIA of a script for Oracle. I have a meeting with the developers this afternoon to show them my final code. I’m expecting them to be fairly pleased.
Thank you all again!!! I learned a lot and sooooo much more to learn!! I’d say i’m a newbie + 1 or maybe 2 :-D.