Where clause not working with Get-ADUSer

I have two problems:

  1. When I use this command and view the variables in Visual Studio Code, I only see 8 variables as opposed to all of them, even though I specified "*" for properties. Interestingly, under the "property count" property of each object, it states 103. I don't know why I can't see them though. Why is this?
    $test=Get-ADUser-filter *-properties *-SearchBase "OU=Service Accounts,DC=blah,DC=blah,DC=blah"
  2. When trying to filter the items with the Where clause, based on the PasswordNeverExpires propery, it does indeed filter the objects, but it isn't giving me what I want. When I go through the results manually and do a Get-ADUser lookup in Powershell ISE on the filtered objects, it states that the PasswordNeverExpires is set to True, even though I specified that I only wanted objects where that property was false.
    $test = $test | Where-Object {$_.PasswordNeverExpires -eq "False"}
    I feel like the first problem is related to the second one so I thought I'd include it. This must be something simple that I'm missing because I've been debugging this for an hour. Can someone please explain to me why I can't see the properties of the object that I specified and why the Where clause is seemingly doing the opposite of what I want?

Even when you specify -Properties * the cmdlet will show you the default output. If you like to see all attributes you should use eiteher a Format-List * or a Select-Object -Property *.
If you like to check for true or false you will have to use the Powershell default variables $true and $false.

BTW: to format code in this forum you should use the code tag button of post editor (pre). Thanks

[quote quote=162557]Even when you specify -Properties * the cmdlet will show you the default output. If you like to see all attributes you should use eiteher a Format-List * or a Select-Object -Property *.

If you like to check for true or false you will have to use the Powershell default variables $true and $false.

BTW: to format code in this forum you should use the code tag button of post editor (pre). Thanks

[/quote]
Thank you for the response. Piping the output to Select-Object -Property * worked. If it did indeed have the values for those properties stored in $test, even if they weren’t clearly visible, they must have been somewhere that I can find within the Variables window pane in Visual Studio Code. If not, then how would piping to Select-Object work if it didn’t have the properties to begin with? Could you explain more in-depth why simply specifying the properties within the Get-ADUSer command was not enough?

I have fixed the code tag above, sorry about that.

That’s strange. Just running $test should show all the properties. I’m not sure what you’re doing in Vscode.

[quote quote=162617]That’s strange. Just running $test should show all the properties. I’m not sure what you’re doing in Vscode.

[/quote]
The code I have above is the entire script, just those two lines. The other commentor indicated that it may just be the way Powershell is. What happens when you run those two lines that I have?

If I do something like this, I see all the properties. I don’t know how to get a variables window in vscode like you do.

$test = Get-ADUser-filter * -properties * -SearchBase "OU=Service Accounts,DC=blah,DC=blah,DC=blah"
$test

[quote quote=162650]If I do something like this, I see all the properties. I don’t know how to get a variables window in vscode like you do.

PowerShell
3 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
$test = Get-ADUser-filter * -properties * -SearchBase "OU=Service Accounts,DC=blah,DC=blah,DC=blah"
$test
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] You have to set a breakpoint and temporarily halt code execution. Set the breakpoint at the first statement, then step over and look at the $test variable in the Variables window

Not true in my env. Using `-Prop *` on an AD cmdlet will show all the properties without `ft` or `select`

You have to set a breakpoint and temporarily halt code execution. Set the breakpoint at the first statement, then step over and look at the $test variable in the Variables window

I don’t get it. Things like Step Over, Step Into, Step Out, Continue, Stop Debugging, and Restart Debugging under Debug are greyed out, and I set a breakpoint and picked Start Debugging.

EDIT: Ok, I did an update and debug seems to work. I don’t know why vscode doesn’t show all the properties in the debug variables window. This seems to be a known bug: Not all properties are displayed during debug in the Variable panel · Issue #821 · PowerShell/vscode-powershell · GitHub This is a problem with VSCODE.

It’s true that in many other cases, the default view doesn’t show all the properties of an object at the command line. But not in this case.

also be aware, if you use format-list, you immediately loose the object.
it will just become text strings and no longer available in the pipeline.

I agree with Ray, in vs code, console, or ise, a -properties * returns every property on the ad-user.

now to roll back to the original problem.

[pre]
$test=Get-ADUser -properties PasswordNeverExpires -filter {PasswordNeverExpires -eq “True”} -SearchBase “OU=Service Accounts,DC=blah,DC=blah,DC=blah”
[/pre]

why this approach? we always want to filter as far left as possible, your initial approach you literally return every user object in that ou, as well as every property possible. You should only retrieve the exact properties you require, and you should filter as early in the pipeline as possible.

That’s so weird. I didn’t think it was even retrieving the variables successfully since the Where clause wasn’t working, but as the above poster stated, I need to use $true or $false, I can’t specify it as a string.

[quote quote=162684][/quote]
I know it’s best to specify only the needed properties. I only specified “*” for testing purposes.