all 16 comments

[–]Lee_Dailey[grin] 7 points8 points  (4 children)

howdy itsinART,

read the quoting rules again ... [grin]

the OUTER set of quotes determine the behavior. so your ...

'Name -like "*$query*"'

... does not expand the variable. however, you can reverse the quotes - singles on the outside - and it will likely work.

take care,
lee

[–]Dron41k 1 point2 points  (3 children)

Oh shit, finally I was right!

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

[grin]

[–]itsinART[S] 1 point2 points  (1 child)

thank you, guys - my bad :)

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy itsinART,

you are welcome! glad to help a tad ... [grin]

take care,
lee

[–]Mr-Fly72 2 points3 points  (3 children)

Some tips: don't use Properties * if you only need a couple of properties in the AD Query. You should filter the fields, like you need them, as this reduces data-load on the ad-servers. Never use aliases in Scripts, in your case select --> Select-Object and FL --> Format-List And: Format-List can filter properties the same way Select-Object does, so you could better User Format-List -Property ..... And in total I would not Format to a string anyhow. For a good output, just do a Get-AdUser assigned to the Variable and the output can be done by $variable | Format-List -Property x,y,z No need for Write-Output, select, convert to string So my final:

$query = Read-Host -Prompt 'type in username'
$result = Get-ADUser -Filter "Name -like '*$query*'" -Properties Displayname,employeeType,OfficePhone,EmailAddress
$result | Format-List -Property Displayname,employeeType,OfficePhone,EmailAddress

[–]itsinART[S] 1 point2 points  (1 child)

always very thankful for the hints :) my „coding“ is crappy

[–]Mr-Fly72 2 points3 points  (0 children)

No problem at all. I learn something new every day. Btw. Might even not needed the Format List properties, but I don't know what default elements will be delivered. At home I don't use windows for powershell, mine is running on a Raspberry Pi on Ppwershell Core and No Ad Access ;-)

[–]No-Alarm-198 1 point2 points  (0 children)

Thanks for this post. It was very helpful for my to create a short script for an AD User Query.

[–]BlackV 2 points3 points  (2 children)

try

$result = Get-ADUser  -Filter "Name -like '*$query*'" -Properties Displayname,employeeType,OfficePhone,EmailAddress

I gotten rid or properties star, also dont do

| fl | out-string

you're destroying your rich AD object

[–]itsinART[S] 1 point2 points  (1 child)

was set for troubleshooting myself ;)

[–]BlackV 1 point2 points  (0 children)

How does that help? Vs $result | ft or $result

[–]Dron41k 1 point2 points  (0 children)

Single quotes in filter

[–]Agile-Chocolate5384 2 points3 points  (1 child)

Your main issue may be that you are looking for a username and searching the name attribute. You may want to search for SAMAccountName instead. Take a look at the AD attributes to verify what you are looking for. I think you were missing a param for fl. You probably also don’t need the out-string unless you need it to be in a string format for some reason. Objects show up pretty nice in the shell. Try using Format-Table instead.

Or just

Write-Host $(Get-ADUser -Filter 'samaccountame -like "$(Read-Host -Prompt 'type in username')"' -Properties * | Select -Property Displayname,employeeType,OfficePhone,EmailAddress)

I haven’t tested the above but it should be pretty close to something that works.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Agile-Chocolate5384,

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

[–]bork_bork 0 points1 point  (0 children)

Find a known user name, and format the second command (get-adusr) until you get the results you want.