Help with script

Frequent Contributor

Hello all

I have the below script. What i am trying to accomplish is this. below. 


  1. csv file contains list of users (upn)

  2. PowerShell script reads all users from csv file (step1.) does a compare or hash table against users in three specific OU's in AD using (upn). The three specific OU's contain all of our vendor accounts

  3. If a match is found, extend account expiration + 90 days

  4. if a match is not found write the non matched accounts to a separate .csv file

However i am getting the following error

"Get-ADUser : Error parsing query: 'userPrincipalName -eq @{}.UPN' Error Message: 'syntax error' at position: '23'.
At line:4 char:15
+ ... $u = Get-ADUser -Filter "userPrincipalName -eq $_.UPN" -Proper ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser"



  1. $OUNames = "OU=1,OU=X,DC=domain,DC=tld", "OU=2,OU=Y,DC=domain,DC=tld", "OU=3,OU=X,DC=domain,DC=tld"
  2. Import-Csv C:\Junk\AllHands.csv |
  3. ForEach-Object{
  4. $u = Get-ADUser -Filter "userPrincipalName -eq $_.UPN" -Properties AccountExpires,distinguishedName
  5. if ($u){
  6. $OU = ( $u.DistinguishedName.Substring($u.DistinguishedName).IndexOf('OU=',[System.StringComparison]::CurrentCultureIgnoreCase) )
  7. if ($OUNames -contains $OU){
  8. Set-ADAccountExpiration -Identity $u.distinguishedName -DateTime ([datetime]::fromfiletime($[0])).AddDays(90)
  9. }
  10. else{
  11. $_
  12. }
  13. }
  14. else {
  15. $_
  16. }
  17. } | Export-Csv C:\Junk\WhoAreThesePeople.csv -NoTypeInformation


2 Replies

The problem might be the way you are getting the UPN value and the single quotes

Maybe you want to try with

Get-ADUser -Filter "userPrincipalName -eq '$_.UPN'"

You can also take a look at this script of mine where I do a similar thing

Do you have a column name in the CSV named UPN right?


Edit: Typo ;)

Yes its upn in the import .csv file. This is finding the users, but What this code appears to be doing is writing whatever it finds in the import .csv file to the WhoAreThesePeople.csv . What i need are the *differences* between what is in the import .csv file and what is found in the OU's to be written to the WhoAreThesePeople.csv file and all matching accounts found in the .csv import and OU's should have their accountexpires + 90 days

$OUNames = "OU=FMI,OU=Cognizant,OU=FM Users,OU=Corp,DC=ip-tech,DC=com", "OU=BPO and RPA,OU=Cognizant,OU=Consultants,OU=Users,OU=Corp,DC=ip-tech,DC=com"
Import-Csv C:\temp\test2.csv |
get-aduser -Filter "userPrincipalName -eq '$($_.upn)'"
if ($u){
$OU = ( $u.DistinguishedName.Substring($u.DistinguishedName).IndexOf('OU=',[System.StringComparison]::CurrentCultureIgnoreCase) )
if ($OUNames -contains $OU){
Set-ADAccountExpiration -Identity $u.distinguishedName -DateTime ([datetime]::fromfiletime($[0])).AddDays(90)
else {
} | Export-Csv C:\temp\WhoAreThesePeople.csv -NoTypeInformation