jes516
jes516

Reputation: 552

Powershell Assign MobilePhone property from Active Directory to variable

I have a small script I wrote but I am wondering if this is the correct way to pull the MobilePhone property from Active Directory using powershell?

$csvfi = import-csv "C:\Users\\Documents\users.csv"

foreach($row in $csvfi)
{
    $cellphone = $row.Phone
    $fullname = $row.Name

    $adphone = (Get-ADUser -Filter "Name -eq '$fullname'" -Properties * | Select MobilePhone).MobilePhone

    Write-Host $fullname, $adphone
}

It just seems cumbersome to do -Filter, then -Properties *, then pipe to Select and then get the .MobilePhone attribute from that object.

As a side note, I just need the raw Mobile Phone number from AD, 1-AAA-NNN-NNNN so I can compare it with the cell phone number in the spread sheet users.csv.

Upvotes: 0

Views: 805

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23385

$adphone = (Get-ADUser -Filter "Name -eq '$fullname'" -Properties MobilePhone).MobilePhone

Upvotes: 1

Related Questions