r/PowerShell•
usbeef

Get-ADUser Filter Help

I am trying to get the following script to only query objects that have a job title. Is that possible?

Get-ADUser -filter * -properties * | select @{Name="Username";Expression={$.EmailAddress}},@{Name="First name";Expression={$.Givenname}},@{Name="Last name";Expression={$.Surname}},@{Name="E-mail";Expression={$.Mail}},@{Name="Job Title";Expression={$.Title}},@{Name="Direct Manager";Expression={(get-aduser -property emailaddress $.manager).emailaddress}} | export-csv "C:\export.csv" -NoTypeInformation

Posted Jan 7, 2022, 11:57 PM UTC

58
Lee_Dailey[grin]•

howdy y'all,

for those who wanna read that hot mess without getting a headache [grin] ...

Get-ADUser -filter * -properties * |
    select @{Name="Username";Expression={$_.EmailAddress}},
        @{Name="First name";Expression={$_.Givenname}},
        @{Name="Last name";Expression={$_.Surname}},
        @{Name="E-mail";Expression={$_.Mail}},
        @{Name="Job Title";Expression={$_.Title}},
        @{Name="Direct Manager";Expression={
            (get-aduser -property emailaddress $_.manager).emailaddress}} |
    export-csv "C:\export.csv" -NoTypeInformation

take care,
lee

6
throwaway09563•

-filter {title -like "*"}

(from memory)

5
64rk•

I believe you are correct though I'd say {title -ne ""} for this case but I'm sure there's better options Edit: I read it wrong - thought it said find accounts with no title

3
usbeefOP•

That did it! Thanks!

2
64rk•

"Friends don't let friends -filter * -property *" - rg edu

2
User20Name•

{title -ne $null}

2
jsiii2010•

The only extra properties you need are "emailaddress, mail, title, manager" instead of "*". I wonder what the difference is between emailaddress, mail, and userprincipalname.

I think emailaddress is a somehow powershell friendly version of mail (it looks the same for me though), and userprincipalname isn't really an email address (I just tried it).

2
Lee_Dailey[grin]•

howdy usbeef,

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 <c>.
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

1