T O P

  • By -

toybits

If you just want an attribute put the command into a subexpression then just reference that attribute. $aduser = (Get-ADUser UserName).SamAccountName


peacefinder

I’d recommend this method. It’s concise and descriptive (once you’re familiar with the syntax anyway). Another option is $ADUser = get-aduser UserName … which captures the user object. You can then later use the whole object or grab properties as needed without calling get-ADUser again: ($ADUser).surname ($ADUser).name ($ADUser).SAMAccountName $ADUser | select-object -property *


[deleted]

[удалено]


peacefinder

Well TIL. Or maybe I knew that and some point and forgot, I dunno. Using parentheses makes it more clear to me, but good to know they’re optional!


Choppin22g

You need the parenthesis around the variable unless you’ve already filled the variable with information. If you initially run the statement and you just want specific information without all the extra stuff. I would recommend encapsulating the statement in parenthesis and then using the . Operator to call the property that you want


[deleted]

[удалено]


Choppin22g

If you load the information into a variable: $user= get-aduser: then you can just use the variable and use the . Operator: $user.name


motsanciens

`Select -ExpandProperty YourPropertyNameHere`


[deleted]

Thank you!


Tpower1000

But be aware, this only works with one property at a time. So you can't do something like | -ExpandProperty surname,name


Tpower1000

But be aware, this only works with one property at a time. So you can't do something like | -ExpandProperty surname,name


[deleted]

How would I do multiple?


Tpower1000

You can eather use multible variables or use the dot notivication like $var.surname


[deleted]

Oh gotcha. Thank you.


FLAFE1995

You can grab inspiration for this here: https://stackoverflow.com/a/71041934/16873491 And use Santiago's answer, with the "calculated properties"


scootscootman1

yeah i would think $var.whatever is a better approach


TumsFestivalEveryDay

``$varthingy = (get-aduser john.doe).samaccountname`` When cmdlets return a table of properties, just encapsulate the command in parenthesis and use dot notation to return only the value you want. This is a universal thing in PowerShell.


ARASquad

I just spent ages figuring out how to do exactly this just the other day. Try… write-host $Variable1.”samaccountname” and see what you get


[deleted]

What u/motsanciens suggested worked for me!


ARASquad

Yeah that’s another way to do it, a bit longer though but should accomplish the same thing. If you’re looking to shorten it and have a way that’s easy to remember, Get-aduser -identity $variable1.”samaccountname”


[deleted]

Yeah, that's good to know. Never know what situation I'll need it for.


scott1138

I recommend you don’t use the expand property as is makes the object useless for other scenarios.


bis

$variable1 = Get-ADUser foo |% samaccountname is what I'd usually write in this situation, because if you've already written the Get-ADUser part, it's the fewest keystrokes to get the property. $variable1 = (Get-ADUser foo).samaccountname is a cleaner expression of intent though. I would *never* write $variable1 = Get-ADUser foo | select -ExpandProperty samaccountname because it is so cumbersome to type, instead preferring $variable1 = Get-ADUser foo | ForEach-Object samaccountname If it's going into a script and I want to avoid using aliases.


evily2k

I did something like this for a local user: `$localAdmin = (get-localuser | where-object {$_.Name -like "*ADMIN"}).name` Domain user could be done like this: `$domainAdmin = (Get-ADUser -Identity "Administrator").SamAccountName`


Lee_Dailey

howdy ctioneTAn, reddit likes to mangle code formatting, so here's some help on how to post code on reddit ... [0] single line or in-line code enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result `looks like this`. kinda handy, that. [*grin*] _[on New.Reddit.com, use the `Inline Code` button. it's [sometimes] 5th from the left & looks like ``. **this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!**]_ [1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here. please remember to set the file/code type on Pastebin! [*grin*] otherwise you don't get the nice code colorization. [2] less simple = use reddit code formatting ... _[on New.Reddit.com, use the `Code Block` button. it's [sometimes] the 12th from the left, & looks like an uppercase `C` in the upper left corner of a square.]_ - one leading line with ONLY 4 spaces - prefix each code line with 4 spaces - one trailing line with ONLY 4 spaces that will give you something like this ... - one leading line with ONLY 4 spaces - prefix each code line with 4 spaces - one trailing line with ONLY 4 spaces the easiest way to get that is ... - add the leading line with only 4 spaces - copy the code to the ISE [or your fave editor] - select the code - tap TAB to indent four spaces - re-select the code [not really needed, but it's my habit] - paste the code into the reddit text box - add the trailing line with only 4 spaces not complicated, but it _is_ finicky. [*grin*] take care, lee


imahe

You can use `-Identity $variable1.samaccountname` in the 2nd Get-ADUser or `select -ExpandProperty samaccountname` with the 1st.


Formal-Sky1779

Or use | %


kibje

Why would you use `foreach-object` ?


Formal-Sky1779

With | % SamAccountName instead of | select you don’t have the title, only the SamAccountName. Apparently that’s depreciated or something, if you see the downvote. But this is a way, like there are many other ways..


kibje

The main issue I would have with that approach is that you are dropping the property into an (implicit) Out-String (or maybe even Write-Host ?) for each object in your pipeline. It is destroying the object (as -expandproperty would do) but not being very clear about this. You might also get some unintended side effects with more complex types when the display does not match the actual object contents, as I think you might be storing the display in your variable, and not the value itself. You are basically doing `Get-ADUser username | Foreach-Object { Out-String SamAccountName }` but this is hidden by the shorthand you are using. Additionally, the use of `ForEach-Object` to iterate over one element just feels dirty :)


Formal-Sky1779

I agree that this is ‘quick and dirty’. Appreciate the explanation, really nice! I used to use this to pipe a property into something really fast and that was just some code for one time use. Not a long term script to use. To me | % is just a way to use when it can be used like doing some ops work, but bulk. Much faster then expand and so..


lueggy

You can use -expandproperty to do that Get-Aduser -Id $id | select-object -expandproperty samaccountname


RamboYouNotForgetMe

1. $variable = some-command 2. $variable | get-member or $variable | gm This shows methods and properties of your object. You need to look for the property you want to retrieve. 3. $variable.propertyname I use **| gm** all the time to quickly see what I can pull from the object.