Add mobile phone number to AD

Hello,
Can someone help me out I want to be able to have a powershell script (very simple) that can take a users SAMACCOUNT and add the mobile number to that account via CSV.
The field I want to populate would be the field under Telephones “Mobile”

Can anyone help me out.

I believe it would need to look something like this
PS1 file
set-aduser -Identity $user.samaccount -officephone $user.ADNumber

CSV File
1 column $user.samaccount (with my usernames there)
1 column $user.ADNumber (with the phone numbers) but I’m not sure that this is the correct setting

My issue is I’m not sure -officephone is the correct field, I think that -office phone will populate what is under the “general” tab correct?

Thanks!

audi911,
Welcome to the forum. :wave:t4:

Why don’t you figure that out by yourself? :wink: You should set up a user where this attribute is populated. Then you simply read this user with PowerShell and inspect the output. With

Get-AdUser -Identitiy 'YourTestUser' -Properties *

You get all possible attributes. :wink:

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

1 Like

Set-ADUser will populate that attribute directly.
Get-Help Set-ADUser will show you what parameter you need to use.

Hi,
there are several attributes where you can enter a phone number.

Check it:

get-aduser Test_user -properties * | select *phone*, *mobile*

Hello guys, thanks for all the great info.
I try to run my script, it doesn’t throw an error but doesn’t populate the field

My PS1 file looks like this

##Loop through all users in CSV
foreach ($user in $csv) {
set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
}

And my CSV has 2 columns

$User.SamAccountName

and

$User.MobilePhone

Under user.samaccountname i have my samaccountname and under user.mobilephone I have my phone number

The script runs but doesn’t populate the Mobile field

Any ideas?

Thank you very much

Based on your code sample, you did not import your CSV.

I am Costanza, Lord of the idiots. I cannot believe I forgot to put the import switch.

I run it now I get an output and this is what I get

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 C:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 C:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

My CSV has the headers like this

A                                               B
$User.SamAccountName	$User.MobilePhone
Firstname.Lastname                 555-555-5555

Your columns should just be called SamAccountName and MobilePhone.

Otherwise, your loop needs to reference them as

$User.'$User.SamAccountName' and $User.'$User.MobilePhone'.

Hello, I tried SamAccountName and MobilePhone as the columns in the CSV, still same error

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser
$csv = import-csv  “C:\AddMobileAD.ps1”
##Loop through all users in CSV
foreach ($user in $csv) {
set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
}

c:\addmobilead.ps1 contents

A                                     B
$User.SamAccountName                  $User.MobilePhone
First.Last                            555-555-5555

Im not really sure why it’s not working, I even did

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

No success.

Hello,
I tried your suggestion I still get the same error.

tySet-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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUser

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 c:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.SetADUserpe or paste code here
$csv = import-csv  “C:\AddMobileAD.ps1”
##Loop through all users in CSV
foreach ($user in $csv) {
set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
}

Contents of my CSV

Column A                   Column B
$User.SamAccountName       $User.MobilePhone
FirstName.LastName         555-555-5555

I’m not really sure what I am doing wrong, I even put

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Nothing changed

Thank you again!
PS I apologize for this very beginner question.

Your CSV should look like this:

SamAccountName,MobilePhone
"FirstName.LastName","555-555-5555"

And you should not name a CSV file *.ps1. :wink:

1 Like

Hello!
So I tried that also I still get an identity error.
Maybe I am not explaining it correct or maybe (most likely) I’m not understanding the replies.
I want to be able to have my CSV have the following lines

Column A in Excel CSV                 Column B in Excel CSV
SamAccountName                        MobilePhone
Username1                             Mobile#1
Username2                             Mobile#2
Username3                             Mobile#3
Etc                                   Etc

My PS1 file that I run in Powershell looks like this

$csv = import-csv  “C:\AddMobileAD.csv”
##Loop through all users in CSV
foreach ($user in $csv) {
set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
}

When I run my script it spits out this error which as far as I can understand it is saying something is wrong after the -Identity switch

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 C:\AddMobileAD.ps1:4 char:22
+ set-aduser -Identity $User.SamAccountName -Mobile $User.MobilePhone
+                      ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Thanks!

Is your CSV file a valid CSV file? You should open it with a text editor - not Excel - and it should look something like I showed before. Plain text with cells wrapped in quotes separated by comma.

To debug your code you could simply output the variables you’re using to the console to check if they are what you expect them to be. :wink:

I figured it out, the problem seems to be the username I was using!
I tried two other usernames and it was working fine, very strange I’ll have to see why my username is not working, I triple checked the sam account name and it’s correct.
In any case, it’s working now as expected about 4 posts ago :slight_smile:
Thank you VVVVVVEEEEEEERRRRRRRRYYYYYYYY much everyone for your support, I have to say this is now my favorite PS forum!