Forum Discussion
Skipster311-1
Nov 04, 2021Iron Contributor
Help with script
Hello all I have the below script. What i am trying to accomplish is this. below. csv file contains list of users (upn) PowerShell script reads all users from csv file (step1.) does a comp...
Nov 04, 2021
Hi,
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
https://github.com/get-itips/M365EnterpriseDemoCustomizer/blob/dev/Customize-M365EnterpriseDemo.ps1
Do you have a column name in the CSV named UPN right?
Edit: Typo 😉
- Skipster311-1Nov 04, 2021Iron ContributorYes 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 |
ForEach-Object{
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($_.properties.accountexpires[0])).AddDays(90)
}
else{
$_
}
}
else {
$_
}
} | Export-Csv C:\temp\WhoAreThesePeople.csv -NoTypeInformation