1

When I run this line in Powershell, for example

get-aduser someUserName -properties department | select department

I get these results:

department
----------------
Information Technology

I would like to just get the following:

Information Technology

joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30
  • You could try adding `Format-Table -HideTableHeaders` – techguy1029 Dec 05 '19 at 00:19
  • 1
    `Select-Object -ExpandProperty department` .... basicst Powershell knowledge!! ;-) – Olaf Dec 05 '19 at 00:24
  • Format-Table -hideTableHeaders looks promising. Can I get this to end up as a string? Maybe using out-string? – joshgoldeneagle Dec 05 '19 at 00:25
  • 1
    Format cmdets like `Format-Table` are only suitable when you like to output something to your console screen. If you like to do further steps you should never use format cmdlets. ... and they should always be the last cmdlet in a pipeline. – Olaf Dec 05 '19 at 00:27
  • 1
    Another method: `(Get-ADUser someUserName -Properties Department).Department` – boxdog Dec 05 '19 at 00:27
  • -ExpandProperty looks good, thank you! If you want write as an answer I will mark that as correct answer – joshgoldeneagle Dec 05 '19 at 00:28
  • You should always read the help for the cmdlets you're using. You should read it completely including the examples. Start with [Select-Object](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-5.1) – Olaf Dec 05 '19 at 00:29

1 Answers1

2

You could simply grab the resulting object and query its property.

In your example:

(Get-ADUser -Identity someUserName -Properties Department).Department

Edit: I've always seen examples using:

| Select-Object -ExpandProperty propertyName

However, I've never been clear on the differences between one method or the other - would be interesting to know if there is any.

I lean towards the former when pulling out single properties and it's also shorter while not compromising in verbosity for a saved script.

  • That looks like it works as well as select-object -expandProperty department. Thank you – joshgoldeneagle Dec 05 '19 at 01:30
  • Looks like the main difference I can find is that using `Object.Property` will return $null if the property doesn't exist whereas using `Select-Object` will return an error. – Freddy Grande Dec 05 '19 at 04:24