Hello. I have got the following to find iems in Azure Ad that are over 90 days old from Microsoft. I then wanted to add exceptions as there will be some devices that I want to keep so came up with the following which works.

$dt = (Get-Date).AddDays(-90)
Get-AzureADDevice -All:$true | Where {($_.ApproximateLastLogonTimeStamp -le $dt) -and ($_.DisplayName -notcontains "TestPC01")} | select-object -Property AccountEnabled, DeviceId, DeviceOSType, DeviceOSVersion, DisplayName, DeviceTrustType, ApproximateLastLogonTimestamp | export-csv C:\90days-summary.csv -NoTypeInformation

All good. Then I tried to import a .csv file to make list for exceptions as that should make things easier nbut the following doesn’t work and all I can think of is that it should do :slight_smile:

$dt = (Get-Date).AddDays(-90)
$exceptions = Import-Csv -Delimiter "," -LiteralPath "C:\90daysexceptions.csv"
$exceptions
Get-AzureADDevice -All:$true | Where {($_.ApproximateLastLogonTimeStamp -le $dt) -and ($_.DisplayName -notcontains $exceptions)} | select-object -Property AccountEnabled, DeviceId, DeviceOSType, DeviceOSVersion, DisplayName, DeviceTrustType, ApproximateLastLogonTimestamp | export-csv C:\90days-summary.csv -NoTypeInformation

I printed the $exception variable tomake sure that it is reading and it does. I called the column name in the csv DisplayName as well so that it matched the value I am looking for in the -notcontains.

I suppose I can add lots of ANDS for each device but can anyone see why the csv list is not working please?

3 Spice ups

I’d try -notmatch and use regex syntax

You have to make the CSV look like this:

$_.DisplayName -notmatch "exception1|exception2|exception3"

Without knowing what your CSV looks like, it’s tough to help.

in general contains / notcontains checks arrays, so you’d swap it.

$exceptions -notcontains $_.DisplayName
2 Spice ups

Thanks Neally. Your post put me on the right path. I think it is the case that is causing the issues so I did the is instead and avoided it altogether:

$exceptions = “testpc01|testpc03”

That worked using -not match

Many thanks. :+1: