I am trying to create a new Active Directory user, but first I check that the user no longer exists with Get-ADUser . I import user data from the HR department and create custom properties:
$newUsers = Import-Csv $csvFile | Select-Object -Property @{n='EmpNum';e={$_.'Employee Number'}}, @{n='UPN';e={$_.'Email Address'}}, @{n='Alias';e={$_.'Email Address'.Split("@")[0]}}
When I view objects from a CSV file, I use the UPN property to search for a user in Active Directory:
foreach ($newUser in $newUsers) { $exists = Get-ADUser -Filter {UserPrincipalName -eq $newUser.UPN} -Properties * -Server $adServer -Credential $adCred ... }
The filter causes an error:
Get-ADUser : Property: 'UPN' not found in object of type: 'System.Management.Automation.PSCustomObject'. At C:\Users\bphillips.NEWHOPEOFIN\Dropbox\Powershell\NewHire\AddNewDSP.ps1:50 char:15 + $exists = Get-ADUser -Filter {UserPrincipalName -eq $newUser.UPN} -Propertie ...
I tried this: -Filter {UserPrincipalName -eq $ ("$ newUser.UPN"), but this does not help; I get another error
Get-ADUser : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again. At C:\Users\bphillips.NEWHOPEOFIN\Dropbox\Powershell\NewHire\AddNewDSP.ps1:50 char:15 + $exists = Get-ADUser -Filter {UserPrincipalName -eq $("$newUser.UPN")} -Prop ...
$newUser is a string, so I donβt understand why it causes a problem. Hard coding of UserPrincipalName looks like " test@ourcompany.com " works, but $newUser.UPN will not work. **
PS C:\> $newUser.UPN.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object
and
PS C:\> $newUser.UPN | gm TypeName: System.String
$newUser.UPN contains a valid string value
PS C:\> $newUser.UPN ypope@ourcompany.net
What do I need to do to get $newUser.UPN to be recognized as a string for a filter parameter? What happens, I donβt understand?