I have to change all UPN in my Active Directory. Some UPN are completely empty an some are filled.
Now I want to fill them all with their sAMAccountName.
My script looks like this:
# Import AD Module
Import-Module ActiveDirectory
# Import CSV into variable $userscsv
#$userscsv = import-csv C:\temp\rename\usernames.csv
$users = Import-Csv -Path C:\temp\rename\usernames.csv
# Loop through CSV and update users if the exist in CVS file
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter "sAMAccountName -eq '$($user.samaccountname)'"
Set-ADUser -userPrincipalName $($users.samaccountname)
}
I get an errorcode:
Get-ADUser : Der Suchfilter wurde nicht erkannt
Bei C:\TEMP\RENAME\User-Rename.ps1:13 Zeichen:12
+ Get-ADUser <<<< -Filter "sAMAccountName -eq '$($user.samaccountname)'"
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : Der Suchfilter wurde nicht erkannt,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Does anyone have an idea?
I would not recommend setting the UPN to the SamAccountName. That's not how it's supposed to be.
If you must do it anyway, you can't use subexpressions in a filter. Assign the value to a variable and use that in the filter expression. You also need to pipe the user object into Set-ADUser to give the cmdlet something to work with:
foreach ($user in $users) {
$acct = $user.SamAccountName
Get-ADUser -Filter "sAMAccountName -eq '$acct'" |
Set-ADUser -userPrincipalName $acct
}
Related
I have a problem importing phone numbers from a CSV file based on email addresses to Active directory using a PowerShell script.
The table contains:
mail;telephoneNumber
toto#domaine.com;88888888
tata#domaine.com;99999999
here’s the code I’m running but it shows me an error message, or I don’t see why there’s this message:
Import-module ActiveDirectory
Import-CSV E: scripts list.csv |
ForEach-Object {
Write-Host "telephoneNumber $($_.telephoneNumber)"
Get-ADUser -Filter "mail -like '$($_.mail)'" |
Set-ADUser -telephoneNumber $_. telephoneNumber}
Here is the error message:
telephoneNumber
Set-ADUser: Unable to find a parameter corresponding to the name «telephoneNumber».
Character E: scripts employeeid.ps1:6: 14
+ Set-ADUser -telephoneNumber $_. telephoneNumber}
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId: NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
NB: I am a beginner in the subject
Thank you well in advance for your help
I tried this code too but still the same problem.
Import-module ActiveDirectory
Import-CSV "E:\scripts\liste.csv" | % {
$telephoneNumber = $_.telephoneNumber
$mail= $ail_.m
Set-ADUser $telephoneNumber -mail $mail
}
The LDAP property telephoneNumber is known as OfficePhone in PowerShell and LDAP property mail has a PowerShell equivalent called EmailAddress.
Cmdlet Set-ADUser does not have a parameter called telephoneNumber, but it does have OfficePhone, so a rewrite of your code would be
Import-Module ActiveDirectory
Import-Csv -Path 'E:\scripts\list.csv' | ForEach-Object {
$user = Get-ADUser -Filter "mail -eq '$($_.mail)'" # or use PS equivalent 'EmailAddress'
if ($user) {
Write-Host "Setting telephoneNumber $($_.telephoneNumber) for $($user.Name)"
$user | Set-ADUser -OfficePhone $_.telephoneNumber
# if you do want to use LDAP property telephoneNumber, you can use below
# $user | Set-ADUser -replace #{telephoneNumber = $($_.telephoneNumber)}
}
else {
Write-Warning "Could not find user with EmailAddress $($_.mail)"
}
}
P.S. you made some typos when posting:
E: scripts list.csv is missing the backslashes
$_. telephoneNumber has a space between the dot and the property name
I'm using a powershell script to try to import email addresses into Active Directory. Here is the script I'm using (full disclosure, I copied this from another post on this site):
Import-module ActiveDirectory
$Users = Import-CSV "C:\Users\Admin\Documents\HPD_Users.csv"
ForEach($User in $Users)
{
Set-ADUser -Identity $User.UserName -EmailAddress $User.Email
}
The error I'm getting is:
Set-ADUser : Cannot find an object with identity: 'Smith, John' under: 'DC=abc,DC=wxyz,DC=com'.
At C:\Users\Admin\Documents\Import_AD_Email.ps1:5 char:5
+ Set-ADUser -Identity $User.UserName -EmailAddress $User.Email
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Smith, John:ADUser) [Set-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.SetAD
User
I think the issue is that we have the users in a specific OU. How do I specify that in the script?
Like Santiago mentioned, looks like the issue here is that $User.UserName is actually the DisplayName attribute in Active Directory.
The best and most performant way to do this would be to run it against the sAMAccountName - but if all you have access to is the DisplayName in your CSV, you could try something along these lines instead;
Import-module ActiveDirectory
$Users = Import-CSV "C:\Users\Admin\Documents\HPD_Users.csv"
ForEach($User in $Users)
{
$ADUser = Get-ADUser -Filter { DisplayName -like $User.UserName } -Properties DisplayName
Write-Host "Updating email for [$($ADUser.DisplayName)] to [$($User.Email)]"
#Set-ADUser $ADUser -EmailAddress $User.Email -WhatIf
}
Finally got it to work using sAMAccountName. Here was my final script:
Import-module ActiveDirectory
$Users = Import-CSV "C:\Users\Admin\Documents\HPD_Users3.csv"
ForEach($User in $Users)
{
Set-ADUser -Identity $User.sAMAccountName -EmailAddress $User.Email
}
And my CSV file looked like this:
Email,sAMAccountName
john.smith#abc.com,jsmith
jane.doe#abc.com,jdoe
I'm not so good at PowerShell. I have this script which worked before that I've have used to update the manager attribute in Active Directory, however, now after just replacing the file its no longer working
$Users = Import-CSV C:\Documents\Managers.csv
ForEach ($User in $Users) {
$user= Get-ADUser -Filter "displayname -eq '$($Users.EmployeeFullName)'"|select -ExpandProperty samaccountname
$manager=Get-ADUser -Filter "displayname -eq '$($Users.'Line Manager Fullname')'"|select -ExpandProperty DistinguishedName
Set-ADUser -Identity $user -Replace #{manager=$manager}
}
when running the script it returns:
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:6 char:22
+ Set-ADUser -Identity $user -Replace #{manager=$manager}
+ ~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
the "EmployeeFullName" and "Line Manager FullName" format in the .csv is set as i.e. joe bloggs, see below CSV file example.
EmployeeFullname Line Manager Fullname
---------------- ---------------------
Kieran Rhodes Tracey Barley
Lewis Pontin Tracey Barley
Lizzy Wilks Rodney Bennett
I also noticed if I remove and try to retype "$Users.EmployeeFullName" it no longer picks up the "EmployeeFullName" from the csv file.
Anyone know what I'm doing wrong here please?
Below would be an example:
$Users = Import-CSV C:\Documents\Managers.csv
ForEach ($User in $Users) {
$user = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'"
$manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName
Set-ADUser -Identity $User -Replace #{manager=$manager}
}
Note: you don't need to dig down to the samAccountName property because Set-ADUser will take the full fidelity object for the -Identity argument. You also don't need to use Select... -ExpandProperty you can just parenthetically dot reference the distinguishedName property.
Also you can use the instancing capability in the AD cmdlets:
$Users = Import-CSV C:\Documents\Managers.csv
ForEach ( $User in $Users )
{
$User = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'" -Properties Manager
$Manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName
$User.Manager = $Manager
Set-ADUser -Instance $User
}
In this case you call back the Manager property set it with a simple assignment statement than give the $User variable as the argument to the -Instance parameter of Set-ADUser
I have a list of users in active directory to which I have to remove the description and office fields. how can i do through poweshell? I try this but not work.
My code path is ok
Import-Module ActiveDirectory
$Users = Import-csv C:\Users\xxxx\Desktop\test.csv
foreach($User in $Users){
Set-ADUser $User.SamAccountName -Description $User.NewDescription
}
my csv
SamAccountName;NewDescription;EmailAddress
xxxxxxxxxx;xxxxxxxxxxx;xxxxxxxxxxxxx#libero.it
Powershell answer
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the
argument, and then try running the command again.
At line:4 char:12
+ Set-ADUser $User.SamAccountName -Description $User.NewDescription
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUs
er
Help will be appreciated
You need to specify your delimiter on your import-csv statement. The default is comma separated, so you can define it like so
$Users = Import-csv C:\Users\xxxx\Desktop\test.csv -Delimiter ';'
Without this, then it's importing as one single column instead of three.
See Import-CSV for more.
It might work?
$users = Import-Csv -Path C:\users1.csv
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter “SamAccountName -eq ‘$($user.samaccountname)'” -Properties * -SearchBase “cn=Users,DC=**********,DC=*****” |
Set-ADUser -Clear description, Office
}
I'm using a PowerShell script to add some information to all users in AD. For some reason, I keep getting an error message if the user has a apostrophe in their name (e.g Tim O'Reilly).
How can I format the script so it will include names with apostrophe ?
My script:
# Import AD Module
Import-Module ActiveDirectory
write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow
# Import CSV into variable $users
$users = Import-Csv -Path C:\Scripts\users.csv
# Loop through CSV and update users if the exist in CVS file
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -SearchBase "DC=My,DC=domain,DC=com" |
Set-ADUser -Company $($user.Email)
}
Write-Host 'done!' -ForegroundColor Green
And this is the error message I'm getting:
Get-ADUser : Error parsing query: 'displayName -eq 'Tim O'Reilly''
Error Message: 'syntax error' at position: '29'. At
C:\Scripts\Update-information\Update-Users-2.ps1:13 char:1
+ Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr
osoft.ActiveDirectory.Management.Commands.GetADUser
I'd really appreciate any help I can get here.
Thank you,
You can use some custom delimiter in your csv file instead of modifying your script. Just divide your data with custom char like ":" (Tim : O'Reilly), and add delimiter switch for import-csv cmdlet, like
$users = Import-Csv -Path C:\Scripts\users.csv -Delimiter :