DevilWAH
DevilWAH

Reputation: 2633

Variable in powershell AD commands issue

I have searched around (including) this forum and seen comments about this but can not get it to work.

I have a variable in PS $user.accountname = Aaron.street"

But as bellow if i type the name in full it works but not using the variable. I have tried so mane suggestions from searching this forum but nothing seems to work.

    PS D:\Lync PS scripts> Get-adUser -Filter "name -like 'Aaron Street'"


DistinguishedName : CN=aaron street,OU=Full_time,OU=Staff,OU=Accounts,OU=.........
Enabled           : True
GivenName         : aaron
Name              : aaron street
ObjectClass       : user
ObjectGUID        : 037c744b-63fa-4a18-adb3-........
SamAccountName    : street
SID               : S-1-5-21-33161136-...........
Surname           : street
UserPrincipalName : street@...........




PS D:\Lync PS scripts> Get-adUser -Filter "name -like '$user.Accountname'"

PS D:\Lync PS scripts> Write-Host $user.AccountName
Aaron Street

What I want is to find the user and then update a property using another variable "$user.number" So my full command would be

get-aduser -filter "name -like '$user.accountname'" | set-aduser -Replace @{Officephone = $user.Number}

Any thoughts on where I am going wrong with using the variables?

Upvotes: 2

Views: 30

Answers (1)

Kohlbrr
Kohlbrr

Reputation: 4069

You need to use a subexpression to expand the variable correctly

Get-adUser -Filter "name -like '$($user.Accountname)'"

Upvotes: 2

Related Questions