VitaminD3
VitaminD3

Reputation: 3

PowerShell - Use variable in another cmdlet

I want to create a variable with the office phone number of an AD user and use this variable in another cmdlet.

$a = Get-AdUser -Filter "name -eq 'User1'" -Properties OfficePhone | FT OfficePhone | Out-String
Set-ADUser User2 -EmailAddress $a

I tried this one, but it doesn't work. Can someone help me?

Best regards

Upvotes: 0

Views: 237

Answers (2)

StegMan
StegMan

Reputation: 531

$a = (Get-ADuser User1 -Properties OfficePhone).OfficePhone
Set-ADUser User2 -EmailAddress $a

The -Properties parameter of Get-ADUser tells the cmdlet to include the properties you specify in addition to the other properties it already includes, such as DistinguishedName,Enabled,GivenName, etc.

Because of this you have to specify which property you want saved to the $a variable by wrapping the cmdlet in parentheses and using a period followed by the property name. This is actually the same thing as:

$a = Get-ADUser User1 -Properties OfficePhone
$b = $a.OfficePhone
Set-ADUser User2 -EmailAddress $b

Upvotes: 1

MysticHeroes
MysticHeroes

Reputation: 164

You want to select the specific property of the User1 object, in this case OfficePhone. Please try that and let me know if it works as I don't currently have the Active Directory modules installed.

$a = Get-AdUser -Filter "name -eq 'User1'" -Properties OfficePhone | Select-Object OfficePhone
Set-ADUser User2 -EmailAddress $a

Upvotes: 0

Related Questions