Timmy Wong
Timmy Wong

Reputation: 11

How to assign specific field to a variable

I would like to assign SamAccountName to a variable while I run Get-ADUser cmdlet in PowerShell, I tried the following command but always return error.

$manager_sam = Get-ADUser -Identity $manager |fl SamAccountName

Upvotes: 1

Views: 503

Answers (2)

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10764

Assuming that $manager contains a string that's valid to be passed as -Identity, you can just use

$manager_sam = (Get-ADUser -Identity $manager).sAMAccountName

Upvotes: 2

Martin Brandl
Martin Brandl

Reputation: 58931

Use the Select-Object instead of the Format-List cmdlet:

$manager_sam = Get-ADUser -Identity $manager | Select-Object -expand SamAccountName

Upvotes: 3

Related Questions