0

I have the following bit of code

$date = ((get-date).addmonths(-3))
Get-ADUser -Filter * -Properties whenChanged| Where-Object {$_.whenChanged -ge $date} | select name

This works fine and gives me a nice list of users that have not had there account updated in 3 months. But I need this as a log in script so I want it to say logicaly

if ((get-aduser <name> -Properties whenChanged) -ge $date) {do something} else {do nothing} 

When ever I try this i get..

 $c = Get-ADUser street | Where-Object {$_.whenChanged -ge $date} | select whenchanged
write-host $c
@{whenchanged=07/09/2015 17:00:30}

I know there is a magic bit of syntax to make it work and I would be great ful for any pointers.

DevilWAH
  • 2,553
  • 13
  • 41
  • 57
  • As an aside: It's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375). – mklement0 Aug 01 '18 at 13:28

1 Answers1

1

Because the Result is an [Hashtable] Type you need to add the -ExpandProperty to the Select-Object Cmdlet to expand it, like this:

$c = Get-ADUser street -Properties whenChanged | Where-Object {$_.whenChanged -ge $date} | 
Select -ExpandProperty whenchanged
write-host $c

-ExpandProperty

Specifies a property to select, and indicates that an attempt should be made to expand that property. Wildcards are permitted in the property name.

For example, if the specified property is an array, each value of the array is included in the output. If the property contains an object, the properties of that object are displayed in the output.

Avshalom
  • 8,657
  • 1
  • 25
  • 43
  • 1
    `-ExpandProperty` is indeed the right answer, but it's not about _arrays_ per se (and in the OP's example a _single_ object is returned); it's about `Select-Object` returning a _custom object_ even if you specify only a single property to extract; `-ExpandProperty` bypasses creation of the custom object and directly returns the requested property value. – mklement0 Aug 01 '18 at 13:30