0

I am currently developing and consolidating my PowerShell skills and needed help for my script.

I want to retrieve only the result / value "true / false" of the object rather than the description followed by the result of value "true / false".

It will be clearer with my code :

$moreadlock = get-aduser -identity fferman -properties * | select lastbadpasswordattempt, lastlogondate, lockedout, passwordexpired, passwordlastset | format-list

    $sAMAccountName = Read-Host "Username"
        if ( $null -ne ([ADSISearcher] "(sAMAccountName=$sAMAccountName)").FindOne() -eq $true ) {
Write-Host "Exist."
Write-Host ""
Write-Host "Additional information about the selected user : "    
$moreadlock

AND at this level, I would like to implement a condition : If the value of "lockedout" is true, propose an unblocking of the account in the AD

        if ( $lockedout -eq $true ) {
Write-Host "Account locked."

With, after asking the user something like that :

$qunlock = Read-Host "Unlock account ?"
    if ( $qunlock -eq $true ) {
    Unlock-AdAccount }

The problem being that using the command:

$test=get-aduser -identity user -properties * | Select-Object -Property lockedout
$test

This returns me:

lockedout
---------
    False

And not the result of the value itself, "false".

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • `(get-aduser -identity user -properties *).lockedout` (called [member enumeration](https://stackoverflow.com/questions/12131416/how-does-member-enumeration-work-in-powershell-3)) – iRon Sep 09 '21 at 07:55
  • 4
    INstead of `Select-Object -Property lockedout` do `Select-Object -ExpandProperty lockedout` – Olaf Sep 09 '21 at 08:21
  • 2
    As in @Olaf 's comment, **don't use** `-Properties *`. It is wasteful and time consuming to ask for **ALL** properties where you really only need one. – Theo Sep 09 '21 at 11:32

2 Answers2

0

I have tested in my environment.

Please use the below command to get the value of the LockedOut of an AD user :

Get-ADUser -Identity "labadmin" -properties * | Select-Object -ExpandProperty lockedout

enter image description here

RamaraoAdapa
  • 2,837
  • 2
  • 5
  • 11
0

Here is the solution that I have chosen to implement.

Write-Host "Is the account blocked : " -ForegroundColor gray -NoNewline
(get-aduser -identity $ADuser -properties *).lockedout

*(get me all the properties of $ADuser account).select the value of this property

Is the account blocked - example

Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39